如何在c#中读取xml文件

本文关键字:读取 xml 文件 | 更新日期: 2023-09-27 18:13:15

我有一个读取xml文件的代码。有些地方我不明白。根据我的理解,代码将创建一个包含2个元素的xml文件,"产品"answers"其他详细信息"。为什么我们只需要使用writer.WriteEndElement();当我们用writer的时候。WriteStartElement两次?我们不是应该互相关闭吗作家。WriteStartElement语句与writer.WriteEndElement()语句?

using System.Xml;
public class Program
{
    public static void Main()
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        XmlWriter writer = XmlWriter.Create("Products.xml", settings);
        writer.WriteStartDocument();
        writer.WriteComment("This file is generated by the program.");
        writer.WriteStartElement("Product");          // first s
        writer.WriteAttributeString("ID", "001");
        writer.WriteAttributeString("Name", "Soap"); 
        writer.WriteElementString("Price", "10.00")  
        // Second Element 
        writer.WriteStartElement("OtherDetails");
        writer.WriteElementString("BrandName", "X Soap");
        writer.WriteElementString("Manufacturer", "X Company");
        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Flush();
        writer.Close();
    }
}
using System;
using System.Xml;
public class Program
{
    public static void Main()
    {
        XmlReader reader = XmlReader.Create("Products.xml");
        while (reader.Read())
        {
            if (reader.NodeType == XmlNodeType.Element
            && reader.Name == "Product")
            {
                Console.WriteLine("ID = " + reader.GetAttribute(0));  
                Console.WriteLine("Name = " + reader.GetAttribute(1));
                while (reader.NodeType != XmlNodeType.EndElement)
                {
                    reader.Read();
                    if (reader.Name == "Price")
                    {
                        while (reader.NodeType != XmlNodeType.EndElement)
                        {
                            reader.Read();
                            if (reader.NodeType == XmlNodeType.Text)
                            {
                                Console.WriteLine("Price = {0:C}", Double.Parse(reader.Value));
                            }
                        }
                        reader.Read();
                    } //end if

                    if (reader.Name == "OtherDetails")
                    {
                        while (reader.NodeType != XmlNodeType.EndElement)
                        {
                            reader.Read();
                            if (reader.Name == "BrandName")
                            {
                                while (reader.NodeType != XmlNodeType.EndElement)
                                {
                                    reader.Read();
                                    if (reader.NodeType == XmlNodeType.Text)
                                    {
                                        Console.WriteLine("Brand Name = " + reader.Value);
                                    }
                                }
                                reader.Read();
                            } //end if
                            if (reader.Name == "Manufacturer")
                            {
                                while (reader.NodeType != XmlNodeType.EndElement)
                                {
                                    reader.Read();
                                    if (reader.NodeType == XmlNodeType.Text)
                                    {
                                        Console.WriteLine("Manufacturer = " + reader.Value);
                                    }
                                }
                            } //end if
                        }
                    } //end if
                } //end while
            } //end if
        } //end while
    }
}

我不明白这部分:

if (reader.Name == "OtherDetails")
{
    while (reader.NodeType != XmlNodeType.EndElement)
    {
        reader.Read();
        if (reader.Name == "BrandName")
        {
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Text)
                {
                    Console.WriteLine("Brand Name = " + reader.Value);
                }
            }

注意到条件while (reader.NodeType != XmlNodeType.EndElement)被使用了两次吗?

为什么我们不必指定

if (reader.NodeType == XmlNodeType.Element for OtherDetails)和Product一样,

like this

if (reader.NodeType == XmlNodeType.Element
&& reader.Name == "OtherDetails")
{} 

如何在c#中读取xml文件

回答第一个问题:

XmlWriter.WriteEndDocument()的MSDN文档说:

关闭所有打开的元素或属性,并使写入器返回到Start状态。

所以它会自动关闭所有打开的元素。事实上,你可以完全删除对WriteEndElement()的调用,它仍然可以正常工作。

正如人们在上面的评论中所说,你也许应该考虑使用Linq-to-XML。

它可以使事情容易得多。例如,要使用Linq-to-XML从程序中创建XML结构,可以这样做:

var doc = new XDocument(
    new XElement("Product",
            new XAttribute("ID", "001"), new XAttribute("Name", "Soap"),
        new XElement("Price", 10.01),
        new XElement("OtherDetails", 
            new XElement("BrandName", "X Soap"),
            new XElement("Manufacturer", "X Company"))));
File.WriteAllText("Products.xml", doc.ToString());

如果要从XML中读取数据,可以使用var doc = XDocument.Load("Filename.xml")从文件中加载XML,然后将数据取出就像下面这样简单:

double price     = double.Parse(doc.Descendants("Price").Single().Value);
string brandName = doc.Descendants("BrandName").Single().Value;

或者(强制转换):

double price     = (double) doc.Descendants("Price").Single();
string brandName = (string) doc.Descendants("BrandName").Single();

(如果您想知道我们究竟是如何像这样转换XElement类型的对象的:这是因为为XElement定义了大量显式转换操作符。)

如果你需要任何直截了当的东西(没有阅读或研究),以下是我所做的:

我最近为我的WinForms菜单栏编写了一个自定义XML解析方法(它有数百个项目,XML是我最好的选择)。

// load the document
// I loaded mine from my C# resource file called TempResources
XDocument doc = XDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes(TempResources.Menu)));
// get the root element
// (var is an auto token, it becomes what ever you assign it)
var elements = doc.Root.Elements();
// iterate through the child elements
foreach (XElement node in elements)
{
     // if you know the name of the attribute, you can call it
     // mine was 'name'
     // (if you don't know, you can call node.Attributes() - this has the name and value)
     Console.WriteLine("Loading list: {0}", node.Attribute("name").Value);
     // in my case, every child had additional children, and them the same
     // *.Cast<XElement>() would give me the array in a datatype I can work with
     // menu_recurse(...) is just a resursive helper method of mine
     menu_recurse(node.Elements().Cast<XElement>().ToArray()));
}

(我的答案也可以在这里找到:使用Linq读取XML文件-尽管不幸的是它不是Linq)

假设您想要读取xml文件,我们需要使用数据集,因为xml文件内部使用数据集转换为数据表。使用以下代码行访问该文件,并将数据集与xml数据绑定。

    DataSet ds=new DataSet();
    ds.ReadXml(HttpContext.Current.Server.MapPath("~/Labels.xml");

数据集由许多数据表组成,这些数据表的数量取决于xml文件中父/子标签的数量