在WPF上使用C#解析XML [英] Parsing XML with C# on WPF

查看:174
本文介绍了在WPF上使用C#解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试在C#中解析无效的XML文件:

I am trying to parse the fallowing XML file in C#:

<?xml version="1.0" encoding="utf-8" ?>
<orders>
  <order>

    <order_date>1/14/2014</order_date>
    <customer_id>001</customer_id>

    <product>
      <product_id>sh_s</product_id>
      <product_qt>8</product_qt>
    </product>

    <product>
      <product_id>sh_m</product_id>
      <product_qt>2</product_qt>
    </product>

    <product>
      <product_id>pa_32s</product_id>
      <product_qt>4</product_qt>
    </product>

    <product>
      <product_id>ja_l</product_id>
      <product_qt>1</product_qt>
    </product>
  </order>
  

  <order>
  <order_date>1/14/2014</order_date>
  <customer_id>002</customer_id>

  <product>
    <product_id>sh_s</product_id>
    <product_qt>10</product_qt>
  </product>

  <product>
    <product_id>sh_m</product_id>
    <product_qt>1</product_qt>
  </product>

  <product>
    <product_id>pa_32s</product_id>
    <product_qt>3</product_qt>
  </product>

  <product>
    <product_id>ja_l</product_id>
    <product_qt>5</product_qt>
  </product>
  </order>



  <order>
    <order_date>1/14/2014</order_date>
    <customer_id>003</customer_id>

    <product>
      <product_id>sh_s</product_id>
      <product_qt>1</product_qt>
    </product>

    <product>
      <product_id>sh_m</product_id>
      <product_qt>2</product_qt>
    </product>

    <product>
      <product_id>pa_32s</product_id>
      <product_qt>4</product_qt>
    </product>

    <product>
      <product_id>ja_l</product_id>
      <product_qt>7</product_qt>
    </product>
  </order>

</orders>

我有以下C#代码:

XmlDocument doc = new XmlDocument();
                    doc.Load(@"C:\Users\Rafael\Desktop\order.xml");

           
                    
                    foreach (XmlNode xn in doc.SelectNodes("/orders/order"))
                    {

                        string orderDate = xn["order_date"].InnerText;
                        string customer = xn["customer_id"].InnerText;
                        int customerID = Int32.Parse(customer);//convert to integer

                        MessageBox.Show(" | " + orderDate + " | " + customer + " | ");

                        foreach (XmlNode item in doc.SelectNodes("/orders/order/product"))
                        {
                            string productID = item["product_id"].InnerText;
                            string productQT = item["product_qt"].InnerText;
                            int product_qt = Int32.Parse(productQT);//convert to integer

                            MessageBox.Show(" | " + productID + " | " + productQT + " | ");
                        }// Product foreach

                        //Acknowledgement of storage
                        //MessageBox.Show("Data stored successfuly!");

                    }// Order foreach

对于每个订单,我想获取日期,客户以及与之关联的所有产品.

For each order I want to get the Date, Customer, and all products associated with it.

当前代码获取第一个订单的日期和客户,然后获取所有订单中所有产品的产品ID,然后为每个订单重复该过程.

Currently the code gets the date and customer of the first order, and then gets the product Ids of ALL products in ALL orders and then repeats the process for each order.

E.G. >对于第一个< order>我从以下所有< order>中获取日期,客户ID,产品ID和其他不需要的产品ID.

E.G. > for the first <order> I get the date, customer ID, product ID, and additional unwanted product IDs from all the <order>s below.

您对如何解决此问题有任何建议吗?

Do you have any suggestion on how to fix this?

谢谢

推荐答案

请尝试以下较小更改:

Hi, please try this minor change:

                    foreach (XmlNode xn in doc.SelectNodes("/orders/order"))
                    {

                        string orderDate = xn["order_date"].InnerText;
                        string customer = xn["customer_id"].InnerText;
                        int customerID = Int32.Parse(customer);//convert to integer

                        MessageBox.Show(" | " + orderDate + " | " + customer + " | ");

                        foreach (XmlNode item in xn.SelectNodes("product"))
                        {
                            string productID = item["product_id"].InnerText;
                            string productQT = item["product_qt"].InnerText;
                            int product_qt = Int32.Parse(productQT);//convert to integer

                            MessageBox.Show(" | " + productID + " | " + productQT + " | ");
                        }// Product foreach

                        //Acknowledgement of storage
                        //MessageBox.Show("Data stored successfuly!");

                    }// Order foreach


这篇关于在WPF上使用C#解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆