Sana Khan

Sana Khan

  • NA
  • 27
  • 4.9k

Insert XML data in database

Oct 14 2018 12:58 AM
In my code i have converted SVG(XML) file into C# objects  after converting it i want to insert the xml data in database but is unable to insert can anyone help.

Below is my code
private void ConvertXmlToClass()
{
try
{
XDocument doc = XDocument.Load(@"C:\Users\sana-user\Documents\Visual Studio 2015\Projects\SVGXMLToJsonApp\SVGXMLToJsonApp\File\3rect.svg");
string jsonText = JsonConvert.SerializeXNode(doc);
dynamic dyn = JsonConvert.DeserializeObject<ExpandoObject>(jsonText);
dynamic svg = dyn.svg;
var result = new Svg
{
g = new List<G>(),
svgStyle = GetString(svg, "@style"),
id = GetString(svg, "@id"),
x = GetString(svg, "@x"),
y = GetString(svg, "@y"),
viewBox = GetString(svg, "@viewBox"),
cssStyle = GetString(((IDictionary<string, object>)svg)["style"] as ExpandoObject, "#text"),
};
foreach (ExpandoObject gObj in svg.g)
{
var g = new G
{
Id = GetString(gObj, "@id"),
};
if (IsPropertyExist(gObj, "rect"))
{
var rect = ((IDictionary<string, object>)gObj)["rect"] as ExpandoObject;
g.Rect = new Rect
{
Class = GetString(rect, "@class"),
id = GetString(rect, "@id"),
height = GetString(rect, "@height"),
width = GetString(rect, "@width"),
x = GetString(rect, "@x"),
y = GetString(rect, "@y"),
};
}
if (IsPropertyExist(gObj, "text"))
{
var txt = ((IDictionary<string, object>)gObj)["text"] as ExpandoObject;
g.Text = new Text
{
Class = GetString(txt, "@class"),
id = GetString(txt, "@id"),
TextData = GetString(txt, "#text"),
transform = GetString(txt, "@transform"),
};
}
result.g.Add(g);
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
private string GetString(ExpandoObject obj, string key)
{
return ((IDictionary<string, object>)obj)[key] as string;
}
private bool IsPropertyExist(dynamic settings, string name)
{
if (settings is ExpandoObject)
return ((IDictionary<string, object>)settings).ContainsKey(name);
return settings.GetType().GetProperty(name) != null;
}


Below is my class 

public class Svg
{
public string id { get; set; }
public string x { get; set; }
public string y { get; set; }
public string viewBox { get; set; }
public string cssStyle { get; set; }
public string svgStyle { get; set; }
public List<G> g { get; set; }
}
public class G
{
public string Id { get; set; }
public Rect Rect { get; set; }
public Text Text { get; set; }
}
public class Text
{
public string id { get; set; }
public string transform { get; set; }
public string Class { get; set; }
public string TextData { get; set; }
}
public class Rect
{
public string id { get; set; }
public string x { get; set; }
public string y { get; set; }
public string Class { get; set; }
public string width { get; set; }
public string height { get; set; }
}


and below is the XML data that i want to insert in database
 
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 22.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="_x30_" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 1366 768" style="enable-background:new 0 0 1366 768;" xml:space="preserve">
<style type="text/css">
.st0{fill:none;stroke:#000000;stroke-width:0.7087;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;}
.st1{fill:#105689;}
.st2{font-family:'ArialMT';}
.st3{font-size:56.5499px;}
.st4{fill:#4554A5;}
.st5{font-size:56.5574px;}
.st6{fill:#2776BB;}
</style>
<g id="LWPOLYLINE">
<rect id="idrect3" x="251.4" y="-0.3" class="st0" width="866" height="300.2"/>
</g>
<g id="LWPOLYLINE_1_">
<rect id="idrect2" x="248.6" y="366.5" class="st0" width="500.3" height="400.2"/>
</g>
<g id="LWPOLYLINE_2_">
<rect id="idrect1" x="811.4" y="364.2" class="st0" width="300.2" height="404.1"/>
</g>
<g id="TEXT">
<text id="idnano" transform="matrix(1 0 0 1 515.7997 166.1773)" class="st1 st2 st3">Nano Tech</text>
</g>
<g id="TEXT_1_">
<text id="idmigalo" transform="matrix(1 0 0 1 420.2463 553.5321)" class="st4 st2 st5">Migalo</text>
</g>
<g id="TEXT_2_">
<text id="idprime" transform="matrix(1 0 0 1 883.9615 567.5667)" class="st6 st2 st5">Prime</text>
</g>
</svg>
 

Answers (1)