-1

What's best way to read XML ?

My xml is like that I'll have a lot of

<A> and <B>, not sure how many will be there and I have to read the whole xml file.

I can not say that the child element like

<180> will be there under <A> for sure. So does the other element.

But if it's there I have to read it.

Should I use LINQ or XPath or XMLReader ? Which one will be better and easier?

        <A>
            <180>20130218</180>  ///180 is here
            <170>5</170>
            <220>20080210</220>
            <730 CLID='AAA' KW='BBB' KW2='INTERNATIONAL'>
                <731>BBB INTERNATIONAL AG</731>
                <732>XXX</732>
                <735>US66</735>
                <734>YYY</734>
            </730>
            <300>
                <301>
                    <320>20071100</320>
                    <310>12345</310>
                </301>
                <330>US</330>
            </300>
        </A>
        <A>                    ///180 is not here
            <170>5</170>
            <220>20080210</220>
            <730 CLID='AAA' KW='BBB' KW2='WORLD'>
                <731>BBB INTERNATIONAL AG</731>
                <734>YYY</734>
            </730>
            <300>
                <301>
                    <320>20071100</320>
                    <310>12345</310>
                </301>
                <330>UK</330>
            </300>
        </A>
        <B>
            <180>20130218</180>
            <170>5</170>
            <220>20080210</220>
            <730 CLID='AAA' KW='BBB' KW2='INTERNATIONAL'>
                <731>BBB INTERNATIONAL AG</731>
                <732>XXX</732>
                <733>JP</733>
                <735>JP66</735>
                <734>YYY</734>
            </730>
            <300>
                <301>
                    <320>20071100</320>
                    <310>12345</310>
                </301>
                <330>JP</330>
            </300>
        </B>
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
kevin
  • 13,559
  • 30
  • 79
  • 104
  • What you use depends upon what you wish to achieve. If you are simply reading the content, fopen() is also sufficient. – Kangkan Oct 25 '11 at 09:20
  • 4
    What do you want to read? How big will the XML be? Can it fit into memory? Which XML parser to use would depend on answers to those questions. – Darin Dimitrov Oct 25 '11 at 09:21
  • 1
    may I ask you what you're supposed to do with this XML? I mean, convert it to Domain Model Object, or what else? Is the element structures known, except for A and B tags? I mean, <301> will always be under <300>? – themarcuz Oct 25 '11 at 09:23
  • XML can be as large as 150 KB. I want to save the data in database. – kevin Oct 25 '11 at 09:25
  • 301 will be always under 300 but it's not always sure to exist. – kevin Oct 25 '11 at 09:26
  • This question has been asked: [What is the best way to parse (big) XML in C# Code?][1] [1]: http://stackoverflow.com/questions/676274/what-is-the-best-way-to-parse-big-xml-in-c-sharp-code – Marcel Valdez Orozco Oct 25 '11 at 09:28
  • 2
    What you have provided as "XML" isn't a welformed XML document at all. Processing it with XML tools most probably will raise errors. More specifically, an element name cannot start with a digit. Therefore, all tags like `<300>` will cause errors to be produced. – Dimitre Novatchev Oct 25 '11 at 13:18

1 Answers1

6

Should I use LINQ or XPath or XMLReader ? Which one will be better and easier?

XDocument (Linq-to-XML) will be the easiest. And it can probably do what you want but you should be clearer about the desired outcome.

XML can be as large as 150 KB.

No problem at all. Only start to consider XmlReader when you reach 150 MB.

H H
  • 263,252
  • 30
  • 330
  • 514