C# 如何在VisualStudio中使用DOM方法读取XML?我的invoiceData.xml正确吗?

C# 如何在VisualStudio中使用DOM方法读取XML?我的invoiceData.xml正确吗?,c#,xml,visual-studio,visual-studio-2017,C#,Xml,Visual Studio,Visual Studio 2017,这是我的家庭作业。所以你会帮很多忙。这里的问题是我甚至不知道我的invoiceData.xml是否正确。如果是,我如何编写代码来读取它们 string url = "../../../invoiceData.xml"; XmlReader reader = XmlReader.Create(url); StringBuilder orderList = new StringBuilder(); orderList.Append("O

这是我的家庭作业。所以你会帮很多忙。这里的问题是我甚至不知道我的invoiceData.xml是否正确。如果是,我如何编写代码来读取它们

        string url = "../../../invoiceData.xml";
        XmlReader reader = XmlReader.Create(url);
        StringBuilder orderList = new StringBuilder();
        orderList.Append("Order List: ").Append(Environment.NewLine);
        int counter = 0;
        while (reader.ReadToFollowing("item"))
        {
            counter++;
            orderList.Append("Invoice counter: " + counter + Environment.NewLine);
            reader.ReadToFollowing("invoiceID");
            orderList.Append("Invoice ID: " + reader.ReadElementContentAsString() + Environment.NewLine);

            reader.ReadToFollowing("invoiceid");
            orderList.Append("Invoice ID: " + reader.ReadElementContentAsString());

            reader.ReadToFollowing("invoicedate");
            orderList.Append("Invoice Date: " + reader.ReadElementContentAsString());
        }``
这第三段代码以我为例。它们就像样板一样,我可以根据自己的解决方案进行修改和个性化。如果可以的话,你能用一种我能理解这些代码的方式来解释它,以便为我自己人格化吗

最好的,
KAI

老师可能希望您使用序列化器类

    string url = "../../invoice1.xml"; // assume args[0] is invoice1.xml
    XmlReader reader = XmlReader.Create(url);
    StringBuilder invoiceList = new StringBuilder();
    invoiceList.Append("Order List:").Append(Environment.NewLine);
    int counter = 0;
    while (reader.ReadToFollowing("order")) //read to each order element
    {
        counter++; //increase the counter
        invoiceList.Append("Order Counter: " + counter + Environment.NewLine);
        reader.ReadToFollowing("orderID"); //move to orderID element
        invoiceList.Append("Order ID: " + reader.ReadElementContentAsString() + Environment.NewLine); // read OrderID element content

        reader.ReadToFollowing("item"); //move to item element
        int itemCount = 1;
        invoiceList.Append("item: "+itemCount + reader.ReadElementContentAsString() + Environment.NewLine); // read item element content

        while (reader.ReadToNextSibling("item")) //move to next item element
        {
            itemCount++;
            invoiceList.Append("item: "+itemCount + reader.ReadElementContentAsString() + Environment.NewLine); // read item element content
        }

        reader.ReadEndElement();
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Serialization;
利用制度全球化;
使用System.IO;
命名空间控制台应用程序1
{
班级计划
{
常量字符串输入\u文件名=@“c:\temp\test.xml”;
常量字符串输出\u文件名=@“c:\temp\test1.xml”;
静态void Main(字符串[]参数)
{
XmlSerializer反序列化器=新的XmlSerializer(typeof(Order));
XmlTextReader=新的XmlTextReader(输入文件名);
顺序=(顺序)反序列化器。反序列化(读取器);
XmlSerializer serializer=新的XmlSerializer(typeof(Order));
StreamWriter writer=新StreamWriter(输出文件名);
序列化器。序列化(编写器,顺序);
writer.Flush();
writer.Close();
writer.Dispose();
}
}
[XmlRoot(“订单”)]
公共阶级秩序
{
public int invoiceid{get;set;}
私有日期时间_invoicedate{get;set;}
公共字符串发票日期
{
获取{return\u invoicedate.ToString(“d/m/yyyy”);}
设置{u invoicedate=DateTime.ParseExact(值,“d/m/yyyy”,CultureInfo.InvariantCulture);}
}
public int sellerid{get;set;}
public int buyerid{get;set;}
公共int-orderid{get;set;}
[XmlElement(“项目”)]
公共列表项{get;set;}
公共十进制shippingcharges{get;set;}
公共十进制invoicetotalcost{get;set;}
}
[XmlRoot(“项目”)]
公共类项目
{
公共int itemid{get;set;}
公共字符串itemname{get;set;}
公共字符串说明{get;set;}
公共整数数量{get;set;}
公共十进制新单价{get;set;}
}
}

您不必给我答案,告诉我如何阅读XML也会很好!除了不知道XML是否正确之外,您的问题到底是什么?当您运行代码时,您是否会遇到错误?如果是,您会遇到什么错误。如果代码运行但给出了意外的结果-那么让我们知道您的期望和得到的结果。您可以使用这样的网站来验证您的XML-如果您使用Google,还有很多其他网站。此外,Google或在此处搜索解析XML-您将得到大量的结果,如:&XML是正确的。这太棒了!我的老师已经批准了这些代码,所以我可以使用它。非常感谢你!有许多读取xml的方法。在您的情况下,我觉得我发布的代码非常简单,可以轻松阅读和修改数据。此外,数据可以很容易地写回一个文件与一些补充说明。感谢回复,我想写回数据使用流方法。我该怎么做?更新了我的代码。
    string url = "../../invoice1.xml"; // assume args[0] is invoice1.xml
    XmlReader reader = XmlReader.Create(url);
    StringBuilder invoiceList = new StringBuilder();
    invoiceList.Append("Order List:").Append(Environment.NewLine);
    int counter = 0;
    while (reader.ReadToFollowing("order")) //read to each order element
    {
        counter++; //increase the counter
        invoiceList.Append("Order Counter: " + counter + Environment.NewLine);
        reader.ReadToFollowing("orderID"); //move to orderID element
        invoiceList.Append("Order ID: " + reader.ReadElementContentAsString() + Environment.NewLine); // read OrderID element content

        reader.ReadToFollowing("item"); //move to item element
        int itemCount = 1;
        invoiceList.Append("item: "+itemCount + reader.ReadElementContentAsString() + Environment.NewLine); // read item element content

        while (reader.ReadToNextSibling("item")) //move to next item element
        {
            itemCount++;
            invoiceList.Append("item: "+itemCount + reader.ReadElementContentAsString() + Environment.NewLine); // read item element content
        }

        reader.ReadEndElement();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Globalization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string INPUT_FILENAME = @"c:\temp\test.xml";
        const string OUTPUT_FILENAME = @"c:\temp\test1.xml";
        static void Main(string[] args)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(Order));
            XmlTextReader reader = new XmlTextReader(INPUT_FILENAME);

            Order order = (Order)deserializer.Deserialize(reader);


            XmlSerializer serializer = new XmlSerializer(typeof(Order));

            StreamWriter writer = new StreamWriter(OUTPUT_FILENAME);
            serializer.Serialize(writer, order);
            writer.Flush();
            writer.Close();
            writer.Dispose();
        }
    }

    [XmlRoot("order")]
    public class Order
    {
        public int invoiceid { get; set; }
        private DateTime _invoicedate { get; set; }

        public string invoicedate
        {
            get { return _invoicedate.ToString("d/m/yyyy"); }
            set { _invoicedate = DateTime.ParseExact(value, "d/m/yyyy", CultureInfo.InvariantCulture); }
        }

        public int sellerid { get; set; }
        public int buyerid { get; set; }
        public int orderid { get; set; }

        [XmlElement("item")]
        public List<Item> items { get; set; }

        public decimal shippingcharges { get; set; }
        public decimal invoicetotalcost { get; set; }

    }
    [XmlRoot("item")]
    public class Item
    {
        public int itemid { get; set; }
        public string itemname { get; set; }
        public string description { get; set; }
        public int quantity { get; set; }
        public decimal newunitprice { get; set; }
    }

}