-2

我想读取所有order_horder_d属性数据。但在我的程序代码上只能显示 order_h 标签数据。下面是我的代码。谢谢

<document>
<order>
<order_h owner="ABC" order_no="96431"/>
    <order_d owner="ABC" order_no="96431" item_no="0153-8196-010">
        <size assoc_id="1274847" size_id="B0153_0150XSXXLNIVINT_VAEC000076_BK"/>
    </order_d>
    <order_d owner="ABC" order_no="96431" item_no="0153-8196-020">
            <size assoc_id="1274847" size_id="B0153_0150XSXXLNIVINT_VAEC000076_BK"/>
    </order_d>
<order_h/>
</order>
<order>
<order_h owner="ABC" order_no="96432">
    <order_d owner="ABC" order_no="96431" item_no="0153-8196-030">
        <size assoc_id="1274847" size_id="B0153_0150XSXXLNIVINT_VAEC000076_BK"/>
    </order_d>
    <order_d owner="ABC" order_no="96431" item_no="0153-8196-040">
            <size assoc_id="1274847" size_id="B0153_0150XSXXLNIVINT_VAEC000076_BK"/>
    </order_d>
<order_h/>
</order>
</document>



 XmlDocument doc = new XmlDocument();
        doc.Load(@"File.xml");

        XmlNode OrderListNode =
 doc.SelectSingleNode("/document");
        XmlNodeList OrderNodeList =
            OrderListNode.SelectNodes("order/order_h");


        foreach (XmlNode node in OrderNodeList)
        {
            Console.WriteLine(node.Attributes.GetNamedItem("owner").Value);
            Console.WriteLine(node.Attributes.GetNamedItem("order_no").Value);
            Console.WriteLine(node.Attributes.GetNamedItem("profoma_po").Value);

            XmlNodeList OrderdNodeList = OrderListNode.SelectNodes("order_h/order_d");
             foreach (XmlNode orderd in OrderdNodeList)
            {
                Console.WriteLine("D " + orderd.Attributes.GetNamedItem("owner").Value);
                Console.WriteLine("D " + orderd.Attributes.GetNamedItem("order_no").Value);
                Console.WriteLine("D " + orderd.Attributes.GetNamedItem("item_no").Value);
            }


            Console.WriteLine("*****************new Line*******************");

        }
4

1 回答 1

0

您需要调用不同对象上SelectNodesorder_d节点。如果你想要order_d从属于 current 的节点node,你需要SelectNodes调用node

foreach (XmlNode node in OrderNodeList)
{
    Console.WriteLine(node.Attributes.GetNamedItem("owner").Value);
    Console.WriteLine(node.Attributes.GetNamedItem("order_no").Value);
    Console.WriteLine(node.Attributes.GetNamedItem("profoma_po").Value);

    XmlNodeList OrderdNodeList = node.SelectNodes("/order_d");
    foreach (XmlNode orderd in OrderdNodeList)
    {
        Console.WriteLine("D " + orderd.Attributes.GetNamedItem("owner").Value);
        Console.WriteLine("D " + orderd.Attributes.GetNamedItem("order_no").Value);
        Console.WriteLine("D " + orderd.Attributes.GetNamedItem("item_no").Value);
    }

    Console.WriteLine("*****************new Line*******************");
}
于 2012-12-18T15:02:47.997 回答