How do I read and parse an XML file in C#?

后端 未结 11 2135
庸人自扰
庸人自扰 2020-11-21 23:03

How do I read and parse an XML file in C#?

相关标签:
11条回答
  • 2020-11-21 23:39

    Here's an application I wrote for reading xml sitemaps:

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms; 
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.Data;
    using System.Xml;
    
    namespace SiteMapReader
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Please Enter the Location of the file");
    
                // get the location we want to get the sitemaps from 
                string dirLoc = Console.ReadLine();
    
                // get all the sitemaps 
                string[] sitemaps = Directory.GetFiles(dirLoc);
                StreamWriter sw = new StreamWriter(Application.StartupPath + @"\locs.txt", true);
    
                // loop through each file 
                foreach (string sitemap in sitemaps)
                {
                    try
                    {
                        // new xdoc instance 
                        XmlDocument xDoc = new XmlDocument();
    
                        //load up the xml from the location 
                        xDoc.Load(sitemap);
    
                        // cycle through each child noed 
                        foreach (XmlNode node in xDoc.DocumentElement.ChildNodes)
                        {
                            // first node is the url ... have to go to nexted loc node 
                            foreach (XmlNode locNode in node)
                            {
                                // thereare a couple child nodes here so only take data from node named loc 
                                if (locNode.Name == "loc")
                                {
                                    // get the content of the loc node 
                                    string loc = locNode.InnerText;
    
                                    // write it to the console so you can see its working 
                                    Console.WriteLine(loc + Environment.NewLine);
    
                                    // write it to the file 
                                    sw.Write(loc + Environment.NewLine);
                                }
                            }
                        }
                    }
                    catch { }
                }
                Console.WriteLine("All Done :-)"); 
                Console.ReadLine(); 
            }
    
            static void readSitemap()
            {
            }
        }
    }
    

    Code on Paste Bin http://pastebin.com/yK7cSNeY

    0 讨论(0)
  • 2020-11-21 23:40

    You can either:

    • Use XmlSerializer class
    • Use XmlDocument class

    Examples are on the msdn pages provided

    0 讨论(0)
  • 2020-11-21 23:48

    If you want to retrive a particular value from an XML file

     XmlDocument _LocalInfo_Xml = new XmlDocument();
                _LocalInfo_Xml.Load(fileName);
                XmlElement _XmlElement;
                _XmlElement = _LocalInfo_Xml.GetElementsByTagName("UserId")[0] as XmlElement;
                string Value = _XmlElement.InnerText;
    
    0 讨论(0)
  • 2020-11-21 23:49

    XmlDocument to read an XML from string or from file.

    XmlDocument doc = new XmlDocument();
    doc.Load("c:\\temp.xml");
    

    or

    doc.LoadXml("<xml>something</xml>");
    

    then find a node below it ie like this

    XmlNode node = doc.DocumentElement.SelectSingleNode("/book/title");
    

    or

    foreach(XmlNode node in doc.DocumentElement.ChildNodes){
       string text = node.InnerText; //or loop through its children as well
    }
    

    then read the text inside that node like this

    string text = node.InnerText;
    

    or read an attribute

    string attr = node.Attributes["theattributename"]?.InnerText
    

    Always check for null on Attributes["something"] since it will be null if the attribute does not exist.

    0 讨论(0)
  • 2020-11-21 23:49

    There are lots of way, some:

    • XmlSerializer. use a class with the target schema you want to read - use XmlSerializer to get the data in an Xml loaded into an instance of the class.
    • Linq 2 xml
    • XmlTextReader.
    • XmlDocument
    • XPathDocument (read-only access)
    0 讨论(0)
提交回复
热议问题