如何在C#中读取xml文件?

时间:2013-05-10 19:58:59

标签: c# xml

我有一个读取xml文件的代码。有些部分我不明白。 根据我的理解,代码将创建一个包含2个元素的xml文件, “产品”和“其他详细信息”。为什么我们只需要使用writer.WriteEndElement(); 有一次我们使用writer.WriteStartElement两次?我们不应该关闭每一个 writer.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所做的那样,

像这样

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

3 个答案:

答案 0 :(得分:3)

回答你的第一个问题:

正如MSDN documentation for XmlWriter.WriteEndDocument()所说:

  

关闭所有打开的元素或属性,并将编写器重新置于“开始”状态。

因此它会自动关闭所有打开的元素。实际上,您可以完全删除对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类型的对象:这是因为a load of explict conversion operators are defined for XElement。)

答案 1 :(得分:0)

如果你需要任何前进的东西(没有阅读或研究),我就是这样做的:

我最近为WinForms的MenuStrip编写了一个自定义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()));
}

(我的答案也可以在这里找到:Reading an XML File With Linq - 虽然遗憾的是不是Linq)

答案 2 :(得分:0)

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

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

DataSet包含许多数据表,这些数据表的数量取决于xml文件中父子标记的数量