none
Using XDocument to read the root element from XML using C# is not showing the root element. RRS feed

  • Question

  • Hi,

    I am new to C# programming and trying to update the xml file using C#. Here when i am trying to get the root element using XDocument it is showing the complete script in the file.

    Below is my code explanation:

    I am having the below function and it is reading the file path from the command line arguments.

    private XDocument doc;

    public void Update(string filepath)
    {
    string filename = Path.GetFileName(filepath);

    doc = xDocument.Load(filepath);

    XElement rootelement = doc.Root;

    }

    Into the filepath variable we are taking the path "E:\\BuilderTest\\COMMON.wxs"

    Then we are loading the file using XDocument.

    But when we are trying to get the rootelement from the file, it is not showing the root element. Instead it is showing the complete data in the file.


    But when i am using XmlDocument() instead of XDocument() I am able to see only the roor element.

    Below is the code using XmlDocument():

    private XmlDocument doc;

    public void Update(string filepath)
    {
    string filename = Path.GetFileName(filepath);

    doc = new XmlDocument();
    doc.Load(filepath);

    XmlElement rootelement = doc.DocumentElement;
    }

    Please help me by providing your valuable inputs on this.

    Friday, September 28, 2018 7:07 AM

Answers

  • First of all: Embed code and XML into your post using the <> button in the toolbar. Post complete XML. Your *.wxs is not valid XML.

    Then: post concise and complete examples.

    And finally: Why haven't you tested my sample? Do you understand the difference between XElement and XmlElement. Especially as "I should get 'Wix' as the root" makes element" makes not really sense...

    btw, slightly modified, that you can see your Wix.

    namespace ConsoleCS
    {
        using System;
        using System.IO;
        using System.Xml;
        using System.Xml.Linq;
    
        public class Program
        {
            public static void Main(string[] args)
            {
                TestXDocument();
                TestXmlDocument();
    
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }
    
            public static void TestXDocument()
            {
                XDocument document = XDocument.Parse(MockXml());
                XElement root = document.Root;
                Console.WriteLine("\n--- TestXDocument");
                Console.WriteLine(root);
                Console.WriteLine("\n--- Root");
                Console.WriteLine(root.Name);
            }
    
            public static void TestXmlDocument()
            {
                TextReader reader = new StringReader(MockXml());
                XmlDocument document = new XmlDocument();
                document.Load(reader);
                XmlElement root = document.DocumentElement;
                Console.WriteLine("\n--- TestXmlDocument");
                Console.WriteLine(root.OuterXml);
                Console.WriteLine("\n--- Root");
                Console.WriteLine(root.Name);
            }
    
            public static string MockXml()
            {
                return @"
                    <Wix xmlns='httpp://......'>
                        <Fragment />
                    </Wix>";
            }
        }
    }

    Friday, September 28, 2018 12:14 PM
  • Hi Satyaprakash,

    Based on your code and related file, I create a simple sample, which works as expect, could you please share a complete code and related xml file via OneDrive. 

    #Wix

    <?xml version="1.0" encoding="utf-8" ?>
    <Wix xmlns="httpp://www.test.com">
      <Fragment>
        <Directory Ref="abc" def="def">
          <component Name="test1">
            <File Id="1" Name="f1">
            </File>
          </component>
        </Directory>
      </Fragment>
    </Wix>

    #Code.

     static void Main(string[] args)
            {
                string filepath = @"D:\Project\VCS\VCSSample\ConsoleApp58\Test.wxs";
                string filename = Path.GetFileName(filepath);
    
                XDocument doc = XDocument.Load(filepath);
    
                XElement rootelement = doc.Root;
    
                Console.ReadKey();
    
            }

    Best regards,

    Zhanglong


    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.

    Monday, October 1, 2018 8:18 AM
    Moderator

All replies

  • What is your problem?

    namespace ConsoleCS
    {
        using System;
        using System.Xml;
        using System.Xml.Linq;
    
        public class Program
        {
            public static void Main(string[] args)
            {
                TestXDocument(@"C:\Temp\test.xml");
                TestXmlDocument(@"C:\Temp\test.xml");
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }
    
            public static void TestXDocument(string fileName)
            {
                XDocument document = XDocument.Load(fileName);                       
                XElement root = document.Root;
                Console.WriteLine("--- TestXDocument");
                Console.WriteLine(root);
            }
    
            public static void TestXmlDocument(string fileName)
            {
                XmlDocument document = new XmlDocument();
                document.Load(fileName);
                XmlElement root = document.DocumentElement;
                Console.WriteLine("--- TestXmlDocument");
                Console.WriteLine(root.OuterXml);
            }
        }
    }
    XmlElement and XElement have different methods and access patterns. But from the XML view point they work the same.
    Friday, September 28, 2018 8:46 AM
  • Hi,

    Thank you for the sample aplication

    For me, when i am using XDocument with the below code:

    private XDocument doc;
    public void Update(string filepath)
    {
    string filename = Path.GetFileName(filepath);
    doc = xDocument.Load(filepath);
    XElement rootelement = doc.Root;
    }

    When i load my .wxs file and using doc.Root to get the root element, i should get "Wix" as the root element. But i am getting the entire code in the file.

    Here is my .wxs file:

    <Wix xmlns="httpp://......">

    <Fragment>

    <Directory Ref ...>

    <component .....>

    <File Id=""...>

    Friday, September 28, 2018 10:57 AM
  • First of all: Embed code and XML into your post using the <> button in the toolbar. Post complete XML. Your *.wxs is not valid XML.

    Then: post concise and complete examples.

    And finally: Why haven't you tested my sample? Do you understand the difference between XElement and XmlElement. Especially as "I should get 'Wix' as the root" makes element" makes not really sense...

    btw, slightly modified, that you can see your Wix.

    namespace ConsoleCS
    {
        using System;
        using System.IO;
        using System.Xml;
        using System.Xml.Linq;
    
        public class Program
        {
            public static void Main(string[] args)
            {
                TestXDocument();
                TestXmlDocument();
    
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }
    
            public static void TestXDocument()
            {
                XDocument document = XDocument.Parse(MockXml());
                XElement root = document.Root;
                Console.WriteLine("\n--- TestXDocument");
                Console.WriteLine(root);
                Console.WriteLine("\n--- Root");
                Console.WriteLine(root.Name);
            }
    
            public static void TestXmlDocument()
            {
                TextReader reader = new StringReader(MockXml());
                XmlDocument document = new XmlDocument();
                document.Load(reader);
                XmlElement root = document.DocumentElement;
                Console.WriteLine("\n--- TestXmlDocument");
                Console.WriteLine(root.OuterXml);
                Console.WriteLine("\n--- Root");
                Console.WriteLine(root.Name);
            }
    
            public static string MockXml()
            {
                return @"
                    <Wix xmlns='httpp://......'>
                        <Fragment />
                    </Wix>";
            }
        }
    }

    Friday, September 28, 2018 12:14 PM
  • Hi Satyaprakash,

    Based on your code and related file, I create a simple sample, which works as expect, could you please share a complete code and related xml file via OneDrive. 

    #Wix

    <?xml version="1.0" encoding="utf-8" ?>
    <Wix xmlns="httpp://www.test.com">
      <Fragment>
        <Directory Ref="abc" def="def">
          <component Name="test1">
            <File Id="1" Name="f1">
            </File>
          </component>
        </Directory>
      </Fragment>
    </Wix>

    #Code.

     static void Main(string[] args)
            {
                string filepath = @"D:\Project\VCS\VCSSample\ConsoleApp58\Test.wxs";
                string filename = Path.GetFileName(filepath);
    
                XDocument doc = XDocument.Load(filepath);
    
                XElement rootelement = doc.Root;
    
                Console.ReadKey();
    
            }

    Best regards,

    Zhanglong


    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.

    Monday, October 1, 2018 8:18 AM
    Moderator
  • hmm, how does that differ from my samples?
    Monday, October 1, 2018 10:16 AM