[c#] Read XML file into XmlDocument

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.

This question is related to c# xml xmldocument

The answer is


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


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);
    }

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

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.


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to xml

strange error in my Animation Drawable How do I POST XML data to a webservice with Postman? PHP XML Extension: Not installed How to add a Hint in spinner in XML Generating Request/Response XML from a WSDL Manifest Merger failed with multiple errors in Android Studio How to set menu to Toolbar in Android How to add colored border on cardview? Android: ScrollView vs NestedScrollView WARNING: Exception encountered during context initialization - cancelling refresh attempt

Examples related to xmldocument

Why "Data at the root level is invalid. Line 1, position 1." for XML Document? Read XML file into XmlDocument Getting specified Node values from XML document XML Document to String How to modify existing XML file with XmlDocument and XmlNode in C# Convert XmlDocument to String XDocument or XmlDocument Read XML Attribute using XmlDocument How to change XML Attribute What is the simplest way to get indented XML with line breaks from XmlDocument?