1

I am a beginner in xml, xquery and xpath. Is it possible to link two separate xml documents together based on a defined relationship between them? for example if I have an xml document containing all the customers of an online retail system like so (shortened example):

<customers>
<customer loyaltyPoints = "20" sex = "male">
    <customerID>1092</customerID>
    <first_name>James</first_name>
    <second_name>Roland</second_name>
    <email_address>jroland@eircom.net</email_address>
    <DOB>
        <day>07</day>
        <month>05</month>
        <year>1970</year>    
    </DOB>
    <orderlist>
        <order>[LINK TO ORDER FROM ORDERS.XML]</order>
    </orderlist>
</customer>

and a second xml file containing a series of orders like so:

<orders>
<order numberOfItems = "2">
    <orderID>384523</orderID>
    <items>
        <item>[LINK TO PRODUCT X IN PRODUCTS.XML]</item>
        <item>[LINK TO PRODUCT Y IN PRODUCTS.XML]</item>
    </items>
</order>
</orders>

Can I link the customer to the order in the other document and vice versa? Many Thanks

4

1 回答 1

2

如果您使用您拥有的 ID 作为链接密钥,您可以执行以下操作:

XmlDocument customerDoc = new XmlDocument();
customerDoc.LoadXml("<customers>
                         <customer loyaltyPoints = \"20\" sex = \"male\">
                         <customerID>1092</customerID>
                         <first_name>James</first_name>
                         <second_name>Roland</second_name>
                         <email_address>jroland@eircom.net</email_address>
                         <DOB>
                             <day>07</day>
                             <month>05</month>
                             <year>1970</year>
                         </DOB>
                         <orderlist>
                             <order>384523</order>
                         </orderlist>
                       </customer>
                    </customers>");

XmlDocument ordersDoc = new XmlDocument();
ordersDoc.LoadXml("<orders>
                       <order numberOfItems = \"2\">
                           <orderID>384523</orderID>
                           <items>
                               <item>[LINK TO PRODUCT X IN PRODUCTS.XML]</item>
                               <item>[LINK TO PRODUCT Y IN PRODUCTS.XML]</item>
                           </items>
                        </order>
                   </orders>");

// Select all "customer" nodes
XmlNodeList nodes = customerDoc.SelectNodes("customers/customer");

foreach (XmlNode node in nodes)
{
     XmlNodeList orderList = node.SelectNodes("orderlist");
     foreach (XmlNode orderNode in orderList)
     {
           string orderId = orderNode.InnerText;
           XmlNode orderInOrderDocNode = ordersDoc.SelectSingleNode("orders/order[orderID='" + orderId + "']");
           Console.WriteLine(orderInOrderDocNode.InnerText);
            }
        }

给定客户文档中的 orderId,这将打印出在 orders.xml 文档中找到的订单节点。

希望这就是你要找的。

于 2012-12-07T19:14:31.800 回答