1

I have following the XML. It has element nodes and text in it. I have to create object of the following class for each element node (only). The name of the element and its value need to be stored in the object. How can I do that?

    public class MyElement
    {
        public string ElementName { get; set; }
        public string ElementValue { get; set; }
    }

CODE

   static void Main(string[] args)
   {

        XDocument pDoc = XDocument.Parse(@"<main>
                                             Direct 1
                                            <sub1>A</sub1>
                                             Direct 2
                                            <sub2>B</sub2>
                                            <sub3>C</sub3>
                                             00
                                         </main>");



        IEnumerable<XNode> nodes = from c in pDoc.Elements().Nodes()
                                      select c;

        IEnumerable<MyElement> entityCollection = nodes.Select(v => new MyElement()
                             {
                                ElementName = v.ToString()
                             }).ToList();


    }

Required result will look like the following

List<MyElement> sampleRequiredList = new List<MyElement>();
sampleRequiredList.Add(new MyElement() { ElementName = "sub1", ElementValue = "A" });
sampleRequiredList.Add(new MyElement() { ElementName = "sub2", ElementValue = "B" });
sampleRequiredList.Add(new MyElement() { ElementName = "sub3", ElementValue = "C" });

UPDATE

Following is the solution based on the selected answer.

        var elementsUsingRoot = pDoc.Root.Elements();
        var nodesUsingRoot = pDoc.Root.Nodes();

        var secondCollection = pDoc.Root.Elements()
                      .Select(x => new MyElement
                      {
                          ElementName = x.Name.LocalName,
                          ElementValue = x.Value
                      });


        //Text Nodes
        IEnumerable<XText> textNodes = from c in pDoc.Root.Nodes()
                                       where c.NodeType == XmlNodeType.Text
                                       select (XText)c;

        //Element Nodes
        IEnumerable<XElement> elementNodes = from c in pDoc.Root.Nodes()
                                             where c.NodeType == XmlNodeType.Element
                                             select (XElement)c;


        //Element Nodes 2
        IEnumerable<XElement> elementNodes2 = from c in pDoc.Root.Elements()
                                              select c;

REFERENCE

  1. How to get XElement's value and not value of all child-nodes?
  2. Best way to create entity objects from xml using LINQ

1 Answer 1

3

Sounds like you want something like:

var entityCollection = doc.Root
                          .Elements()
                          .Select(x => new MyElement {
                                           ElementName = x.Name.LocalName,
                                           ElementValue = x.Value
                                       });
4
  • Thanks. Can you explain why XText are not retrieved when doc.Root is called? That is the point I am not clear about, now.
    – LCJ
    Mar 13, 2013 at 10:33
  • @Lijo: What do you mean? XText nodes aren't elements.
    – Jon Skeet
    Mar 13, 2013 at 10:35
  • I get XText also in the following query from c in pDoc.Elements().Nodes() select c;. So, I expected XText in the result of doc.Root.Elements()
    – LCJ
    Mar 13, 2013 at 10:38
  • 1
    @Lijo: No, precisely because they're text nodes, not elements. If you used doc.Root.Nodes() you'd get them.
    – Jon Skeet
    Mar 13, 2013 at 10:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.