Read XML file into XmlDocument

2019-03-09 10:15发布

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.

5条回答
不美不萌又怎样
2楼-- · 2019-03-09 10:49

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.

查看更多
我想做一个坏孩纸
3楼-- · 2019-03-09 10:54
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);
    }
查看更多
对你真心纯属浪费
4楼-- · 2019-03-09 10:56

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;
查看更多
迷人小祖宗
5楼-- · 2019-03-09 11:00
XmlDocument doc=new XmlDocument(); 
Doc.Loadxml(@"c:\abc.xml");
查看更多
时光不老,我们不散
6楼-- · 2019-03-09 11:12

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

查看更多
登录 后发表回答