Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a SVG file in XML format i am trying to deserialize XML to C# objects but when trying to deserialize XML data it is giving error

Below is my code

XmlSerializer serializer = new XmlSerializer(typeof(List<rootobject>));
object objectList = null;
using (StreamReader stream = new StreamReader(@"C:\Users\sana-user\Documents\Visual Studio 2015\Projects\SVGXMLToJsonApp\SVGXMLToJsonApp\File\3rect.svg"))
{
objectList = serializer.Deserialize(stream);
}
Console.WriteLine(objectList);



Below is my class

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2000/svg")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.w3.org/2000/svg", IsNullable = false)]
    public partial class svg
    {

        private svgStyle styleField;

        private svgG[] gField;

        private string[] textField;

        private decimal versionField;

        private string idField;

        private string xField;

        private string yField;

        private string viewBoxField;

        private string style1Field;

        private string spaceField;

        public svgStyle style
        {
            get
            {
                return this.styleField;
            }
            set
            {
                this.styleField = value;
            }
        }

        [System.Xml.Serialization.XmlElementAttribute("g")]
        public svgG[] g
        {
            get
            {
                return this.gField;
            }
            set
            {
                this.gField = value;
            }
        }

        [System.Xml.Serialization.XmlTextAttribute()]
        public string[] Text
        {
            get
            {
                return this.textField;
            }
            set
            {
                this.textField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public decimal version
        {
            get
            {
                return this.versionField;
            }
            set
            {
                this.versionField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string id
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string x
        {
            get
            {
                return this.xField;
            }
            set
            {
                this.xField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string y
        {
            get
            {
                return this.yField;
            }
            set
            {
                this.yField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string viewBox
        {
            get
            {
                return this.viewBoxField;
            }
            set
            {
                this.viewBoxField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute("style")]
        public string style1
        {
            get
            {
                return this.style1Field;
            }
            set
            {
                this.style1Field = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/XML/1998/namespace")]
        public string space
        {
            get
            {
                return this.spaceField;
            }
            set
            {
                this.spaceField = value;
            }
        }
    }

    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2000/svg")]
    public partial class svgStyle
    {

        private string typeField;

        private string valueField;

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string type
        {
            get
            {
                return this.typeField;
            }
            set
            {
                this.typeField = value;
            }
        }

        [System.Xml.Serialization.XmlTextAttribute()]
        public string Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2000/svg")]
    public partial class svgG
    {

        private svgGText textField;

        private svgGRect rectField;

        private string[] textField1;

        private string idField;

        public svgGText text
        {
            get
            {
                return this.textField;
            }
            set
            {
                this.textField = value;
            }
        }

        public svgGRect rect
        {
            get
            {
                return this.rectField;
            }
            set
            {
                this.rectField = value;
            }
        }

        [System.Xml.Serialization.XmlTextAttribute()]
        public string[] Text
        {
            get
            {
                return this.textField1;
            }
            set
            {
                this.textField1 = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string id
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }
    }

    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2000/svg")]
    public partial class svgGText
    {

        private string idField;

        private string transformField;

        private string classField;

        private string valueField;

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string id
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string transform
        {
            get
            {
                return this.transformField;
            }
            set
            {
                this.transformField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string @class
        {
            get
            {
                return this.classField;
            }
            set
            {
                this.classField = value;
            }
        }

        [System.Xml.Serialization.XmlTextAttribute()]
        public string Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2000/svg")]
    public partial class svgGRect
    {

        private string idField;

        private decimal xField;

        private decimal yField;

        private string classField;

        private decimal widthField;

        private decimal heightField;

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string id
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public decimal x
        {
            get
            {
                return this.xField;
            }
            set
            {
                this.xField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public decimal y
        {
            get
            {
                return this.yField;
            }
            set
            {
                this.yField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string @class
        {
            get
            {
                return this.classField;
            }
            set
            {
                this.classField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public decimal width
        {
            get
            {
                return this.widthField;
            }
            set
            {
                this.widthField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public decimal height
        {
            get
            {
                return this.heightField;
            }
            set
            {
                this.heightField = value;
            }
        }
    }


Error is coming on line objectList = serializer.Deserialize(stream);
Error is {"<svg xmlns='http://www.w3.org/2000/svg'> was not expected."}


Below is my XML data

XML
<?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>


What I have tried:

Tried many things but i am not getting why this error is coming. Can anyone please help me out to solve this error. I am stuck with this error since a week and unable to solve it.
Posted
Updated 9-Oct-18 21:30pm
v5
Comments
Mehdi Gholam 4-Oct-18 6:36am    
Why convert a .svg file to json or even c# objects?
Member 13041059 4-Oct-18 6:41am    
Want to insert json data in database and the data will be coming through .svg file only in my project
F-ES Sitecore 4-Oct-18 6:42am    
Just deserialise the XML to your c# objects direct, going via json is pointless.
Maciej Los 10-Oct-18 13:26pm    
There's something wrong with your xml. xsd.exe tool can not create a schema file for this. Please, try it by yourself: How to: Use the XML Schema Definition Tool to Generate Classes and XML Schema Documents

1 solution

Your XML is the following:
<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">

You have the following in your class:
public class Svg
...
public Style style { get; set; }
...
public class Style
...

You have two different versions of "style" in your XML, but only one version in your C#.

This is correct, it captures the style tag:
public class Style

public Style style { get; set; }

To capture the attribute you should add this to the Svg class:
public string style { get; set; }


Hope that helps!
 
Share this answer
 
v6
Comments
Member 13041059 5-Oct-18 6:00am    
When i added public string style { get; set; } it is giving error on same line
var obj = JsonConvert.DeserializeObject<rootobject>(jsondata);

{"Unexpected character encountered while parsing value: {. Path 'svg.style', line 9, position 12."}

Below is my SVG class

public class Svg
{
public string version { get; set; }
public string id { get; set; }
public string xmlns { get; set; }
public string __invalid_name__xmlnsxlink { get; set; }
public string x { get; set; }
public string y { get; set; }
public string viewBox { get; set; }
public Style style1 { get; set; }
public string style { get; set; }
public string __invalid_name__xmlspace { get; set; }
public List<string> significantwhitespace { get; set; }
public List<g> g { get; set; }
}
TommoDotCommo 5-Oct-18 21:39pm    
You should output the JSON data in the meantime so we can see what it looks like after the XML to JSON conversion.
Member 13041059 7-Oct-18 1:26am    
{/* Generator: Adobe Illustrator 22.0.0, SVG Export PlugIn . 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":"enablebackground:new 0 0 1366 768;","xml:space":"preserve","significantwhitespace":["
","
","
","
","
","
","
","
"],"style":{"type":"text/css","text":"
.st0{fill:none;stroke:000000;strokewidth:0.7087;strokelinecap:round;strokelinejoin:round;strokemiterlimit:10;}
.st1{fill:105689;}
.st2{fontfamily:'ArialMT';}
.st3{fontsize:56.5499px;}
.st4{fill:4554A5;}
.st5{fontsize:56.5574px;}
.st6{fill:2776BB;}
"},"g":[{"id":"LWPOLYLINE","significantwhitespace":["
","
"],"rect":{"id":"idrect3","x":"251.4","y":"0.3","class":"st0","width":"866","height":"300.2"}},{"id":"LWPOLYLINE1","significantwhitespace":["
","
"],"rect":{"id":"idrect2","x":"248.6","y":"366.5","class":"st0","width":"500.3","height":"400.2"}},{"id":"LWPOLYLINE2","significantwhitespace":["
","
"],"rect":{"id":"idrect1","x":"811.4","y":"364.2","class":"st0","width":"300.2","height":"404.1"}},{"id":"TEXT","significantwhitespace":["
","
"],"text":{"id":"idnano","transform":"matrix(1 0 0 1 515.7997 166.1773)","class":"st1 st2 st3","text":"Nano Tech"}},{"id":"TEXT1","significantwhitespace":["
","
"],"text":{"id":"idmigalo","transform":"matrix(1 0 0 1 420.2463 553.5321)","class":"st4 st2 st5","text":"Migalo"}},{"id":"TEXT2","significantwhitespace":["
","
"],"text":{"id":"idprime","transform":"matrix(1 0 0 1 883.9615 567.5667)","class":"st6 st2 st5","text":"Prime"}}]}}

Above is Json data in jsondata variable
TommoDotCommo 7-Oct-18 18:24pm    
What you have there is invalid JSON. Style is duplicated on the same level which is not valid and there is no way you'll ever get JsonConvert to deserialize it into a C# object. You now need to rethink why you're using JSON at all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900