none
What is difference between XElement & XDocument class RRS feed

  • Question

  • where to use XDocument and when to use XElement .

    please discuss with a small example. also tell me the difference between two class. thanks

    Tuesday, July 30, 2019 3:02 PM

Answers

  • The following page at docs.microsoft.com provides a good explanation along with examples:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/querying-an-xdocument-vs-querying-an-xelement

    As you can see from the examples, the main difference is that you get an extra root node when you query the XDocument, as compared to the XElement.

    • Marked as answer by Sudip_inn Wednesday, July 31, 2019 11:02 AM
    Tuesday, July 30, 2019 3:43 PM
    Moderator
  • Hi Sudip_inn,

    Thank you for posting here.

    Based on your description, you want to know the difference between XElement and XDoceument class.

    As usual, XDocument is used to load xml file. XElement is used to get xml element from XDocument,

    I make a code example, you could have a look.

                XDocument doc = XDocument.Load("D:\\test.xml"); //Load xml file
                XElement xe = doc.Descendants("Student").FirstOrDefault();
                Console.WriteLine(xe);    //Get Elemnt xml string
                Console.WriteLine(xe.Value); //Get Element value

    Xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <body>
      <Shchool>
        <Student>TestStudent</Student>
        <Teacher>TestTeacher</Teacher>
      </Shchool>
    </body>

    Result:

    If you want to know about it, you could refer to XDocument Class or XElement Class.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, July 31, 2019 11:02 AM
    Wednesday, July 31, 2019 1:34 AM
    Moderator

All replies

  • The following page at docs.microsoft.com provides a good explanation along with examples:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/querying-an-xdocument-vs-querying-an-xelement

    As you can see from the examples, the main difference is that you get an extra root node when you query the XDocument, as compared to the XElement.

    • Marked as answer by Sudip_inn Wednesday, July 31, 2019 11:02 AM
    Tuesday, July 30, 2019 3:43 PM
    Moderator
  • Hi Sudip_inn,

    Thank you for posting here.

    Based on your description, you want to know the difference between XElement and XDoceument class.

    As usual, XDocument is used to load xml file. XElement is used to get xml element from XDocument,

    I make a code example, you could have a look.

                XDocument doc = XDocument.Load("D:\\test.xml"); //Load xml file
                XElement xe = doc.Descendants("Student").FirstOrDefault();
                Console.WriteLine(xe);    //Get Elemnt xml string
                Console.WriteLine(xe.Value); //Get Element value

    Xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <body>
      <Shchool>
        <Student>TestStudent</Student>
        <Teacher>TestTeacher</Teacher>
      </Shchool>
    </body>

    Result:

    If you want to know about it, you could refer to XDocument Class or XElement Class.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, July 31, 2019 11:02 AM
    Wednesday, July 31, 2019 1:34 AM
    Moderator