Home > Software design >  Is there smart way to parse xml to list?
Is there smart way to parse xml to list?

Time:09-28

There is xml file, which have next structure:

<?xml version="1.0"?>
<myobjectlist objectCount = "3">
    <myobject Number = "0" Name="My First Object" MyChildObectsCount = "2">
        <intProp>5</intProp>
        <stringProp>Str1</stringProp>
        <doubleProp>35.1</doubleProp>
        <MyChildObj Number = "100" Name = "My first child object">
            <childIntProp>1</childIntProp>
            <childStringProp>CStr1</childStringProp>
        </MyChildObj>
        <MyChildObj Number = "120" Name = "My child object">
            <childIntProp>15</childIntProp>
            <childStringProp>CStr2</childStringProp>
        </MyChildObj>
    </myobject>
    <myobject Number = "145" Name="My second Object" MyChildObectsCount = "1">
        <intProp>96</intProp>
        <stringProp>Str2</stringProp>
        <doubleProp> Inf</doubleProp>
        <MyChildObj Number = "250" Name = "This's child object">
            <childIntProp>62</childIntProp>
            <childStringProp>CStr3</childStringProp>
        </MyChildObj>
    </myobject>
    <myobject Number = "261" Name="My last Object" MyChildObectsCount = "3">
        <intProp>9</intProp>
        <stringProp>Str45</stringProp>
        <doubleProp>1.6449635e 07</doubleProp>
        <MyChildObj Number = "150" Name = "Almost last child object">
            <childIntProp>-1</childIntProp>
            <childStringProp>CStr41</childStringProp>
        </MyChildObj>
        <MyChildObj Number = "680" Name = "Prelast child object">
            <childIntProp>72</childIntProp>
            <childStringProp>CStr42</childStringProp>
        </MyChildObj>
        <MyChildObj Number = "127" Name = "Last child object">
            <childIntProp>64</childIntProp>
            <childStringProp>CStr222</childStringProp>
        </MyChildObj>
    </myobject>
</myobjectlist>

I've tried to use XMLSerializer, but how I found out it can't deserialize to list, all objects of the list will be the first object of deserializable xml, so me result list will consist ObjCount of 0th obj.

I've created classes -- enteties of xml objects, let's say

public class MyObject{
    int Number;
    string Name;
    int IntProp;
    string stringProp;
    double doubleProp;
    List<MyChildObject> myChildObjects;
}

public class MyChildObject{
    int Number;
    string Name;
    int childIntProp;
    string childStringProp;
}

I need to get List<MyObject> from XML file, but I do not want to parse it node by node. Is there smart way to do it?

UPDATE

And here, what I got: deserialization works


UPDATE #2

If you need to handle

  • Inf as float.PositiveInfinity
  • -Inf as float.NegativeInfinity

then you need to change the text of the <doubleProp>. One way to do this is the following:

var malformedXml = File.ReadAllText("sample.xml");
var fixedXml = malformedXml
    .Replace("<doubleProp> Inf</doubleProp>", "<doubleProp>Infinity</doubleProp>")
    .Replace("<doubleProp>-Inf</doubleProp>", "<doubleProp>-Infinity</doubleProp>");

var fixedXmlStream = new MemoryStream();
var writer = new StreamWriter(fixedXmlStream);
writer.Write(fixedXml);
writer.Flush();
fixedXmlStream.Position = 0;
var reader = new StreamReader(fixedXmlStream);
  • Related