Read XML file into XmlDocument

C#XmlXmldocument

C# Problem Overview


I am very new to C#. I have XML file (text.xml). I want to read that in XmlDocument and store the stream in string variable.

C# Solutions


Solution 1 - C#

Use XmlDocument.Load() method to load XML from your file. Then use XmlDocument.InnerXml property to get XML string.

XmlDocument doc = new XmlDocument();
doc.Load("path to your file");
string xmlcontents = doc.InnerXml;

Solution 2 - C#

If your .NET version is newer than 3.0 you can try using System.Xml.Linq.XDocument instead of XmlDocument. It is easier to process data with XDocument.

Solution 3 - C#

XmlDocument doc = new XmlDocument();
   doc.Load("MonFichierXML.xml");

    XmlNode node = doc.SelectSingleNode("Magasin");

    XmlNodeList prop = node.SelectNodes("Items");

    foreach (XmlNode item in prop)
    {
        items Temp = new items();
        Temp.AssignInfo(item);
        lstitems.Add(Temp);
    }

Solution 4 - C#

Hope you dont mind Xml.Linq and .net3.5+

XElement ele = XElement.Load("text.xml");
String aXmlString = ele.toString(SaveOptions.DisableFormatting);

Depending on what you are interested in, you can probably skip the whole 'string' var part and just use XLinq objects

Solution 5 - C#

var doc = new XmlDocument(); 
doc.Loadxml(@"c:\abc.xml");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAJPView Question on Stackoverflow
Solution 1 - C#Timur SadykovView Answer on Stackoverflow
Solution 2 - C#PupperView Answer on Stackoverflow
Solution 3 - C#user3626085View Answer on Stackoverflow
Solution 4 - C#Abdul HfudaView Answer on Stackoverflow
Solution 5 - C#user4679003View Answer on Stackoverflow