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

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

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

相关标签:
11条回答
  • 2020-11-21 23:23
      public void ReadXmlFile()
        {
            string path = HttpContext.Current.Server.MapPath("~/App_Data"); // Finds the location of App_Data on server.
            XmlTextReader reader = new XmlTextReader(System.IO.Path.Combine(path, "XMLFile7.xml")); //Combines the location of App_Data and the file name
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        break;
                    case XmlNodeType.Text:
                        columnNames.Add(reader.Value);
                        break;
                    case XmlNodeType.EndElement:
                        break;
                }
            }
        }
    

    You can avoid the first statement and just specify the path name in constructor of XmlTextReader.

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

    You could use a DataSet to read XML strings.

    var xmlString = File.ReadAllText(FILE_PATH);
    var stringReader = new StringReader(xmlString);
    var dsSet = new DataSet();
    dsSet.ReadXml(stringReader);
    

    Posting this for the sake of information.

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

    There are different ways, depending on where you want to get. XmlDocument is lighter than XDocument, but if you wish to verify minimalistically that a string contains XML, then regular expression is possibly the fastest and lightest choice you can make. For example, I have implemented Smoke Tests with SpecFlow for my API and I wish to test if one of the results in any valid XML - then I would use a regular expression. But if I need to extract values from this XML, then I would parse it with XDocument to do it faster and with less code. Or I would use XmlDocument if I have to work with a big XML (and sometimes I work with XML's that are around 1M lines, even more); then I could even read it line by line. Why? Try opening more than 800MB in private bytes in Visual Studio; even on production you should not have objects bigger than 2GB. You can with a twerk, but you should not. If you would have to parse a document, which contains A LOT of lines, then this documents would probably be CSV.

    I have written this comment, because I see a lof of examples with XDocument. XDocument is not good for big documents, or when you only want to verify if there the content is XML valid. If you wish to check if the XML itself makes sense, then you need Schema.

    I also downvoted the suggested answer, because I believe it needs the above information inside itself. Imagine I need to verify if 200M of XML, 10 times an hour, is valid XML. XDocument will waste a lof of resources.

    prasanna venkatesh also states you could try filling the string to a dataset, it will indicate valid XML as well.

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

    Linq to XML.

    Also, VB.NET has much better xml parsing support via the compiler than C#. If you have the option and the desire, check it out.

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

    LINQ to XML Example:

    // Loading from a file, you can also load from a stream
    var xml = XDocument.Load(@"C:\contacts.xml");
    
    
    // Query the data and write out a subset of contacts
    var query = from c in xml.Root.Descendants("contact")
                where (int)c.Attribute("id") < 4
                select c.Element("firstName").Value + " " +
                       c.Element("lastName").Value;
    
    
    foreach (string name in query)
    {
        Console.WriteLine("Contact's Full Name: {0}", name);
    }
    

    Reference: LINQ to XML at MSDN

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

    Check out XmlTextReader class for instance.

    0 讨论(0)
提交回复
热议问题