0

I know there are many questions on this very topic but none of the examples I've tried have worked for me.

I have an existing XML document that is constructed like so:

<root>
    <ElementA>
        <element1>sometext</element1>
        <element2>sometext</element2>
        <element3>sometext</element3>
    </ElementA>

    <ElementA>
        <element1>sometext</element1>
        <element2>sometext</element2>
        <element3>sometext</element3>
    </ElementA>

    <ElementB>
        <element1>sometext</element1>
        <element2>sometext</element2>
        <element3>sometext</element3>
        <element4>sometext</element4>
        <element5>sometext</element5>
    </ElementB>

    <ElementB>
        <element1>sometext</element1>
        <element2>sometext</element2>
        <element3>sometext</element3>
        <element4>sometext</element4>
        <element5>sometext</element5>
    </ElementB>

    <ElementC>
        <element1>sometext</element1>
        <element2>sometext</element3>
    </ElementC>

    <ElementC>
        <element1>sometext</element1>
        <element2>sometext</element2>
    </ElementC>
</root>

I need to find all the ElementA, B, and Cs and create a new object of type objA, etc. whose properties correspond to the respective element's child elements. Below is example code that I've tried.

var doc = XElement.Load(filename);
var data = from ele in doc.Elements()
           where ele.Name.LocalName.Equals("ElementA")
           select new objA
           {
                name = ele.Element("element1").Value
                address = ele.Element("element2").Value
           };

But this throws a null exception error when I try iterating through "data" collection. What am I not understanding?

1 Answer 1

1

You can get elements by its name using Elements("tagName"):

var data = from ele in doc.Elements("ElementA")
           select new objA
           {
               name = (string)ele.Element("element1"),
               address = (string)ele.Element("element2")
           };

You also should use (string)XElementInstance instead of XElementInstance.Value, to avoid NullReferenceException when element does not exist in XML.

Code pasted above returns 2 elements for your sample input data.

Sign up to request clarification or add additional context in comments.

8 Comments

Doesn't work for me. "data" ends up null. I had tried your solution previously but it seems like doc.Elements("ElementA") doesn't find ElementA. That's why I had to use the "where ele.Name.LocalName.Equals()"
So your real data has to be incorrect. I've tested it with your sample one and works fine.
True, I am wondering if there is something wonky with this XML doc. The example I gave is not the original data but represents the logical layout. Unfortunately this particular XML file I am forced to work with. I've successfully parsed if previously using foreach loops and switch/cases but it's very ugly.
The only think I have on my mind is a case, when your ElementA is not directly under root element. You should use Descendants instead of Elements when that is your case.
Does your real document have a namespace by any chance?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.