O'Reilly logo

C# Cookbook by Jay Hilyard, Stephen Teilhet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

17.9. Tearing Apart an XML Document

Problem

You have an XML document that needs to be broken apart into its constituent parts. Each part can then be sent to a different destination (possibly a web service) to be processed individually. This solution is useful when you have a large document, such as an invoice, in XML form. For example, with an invoice, you would want to tear off the billing information and send this to accounting while sending the shipping information to shipping, and then send the invoice items to fulfillment to be processed.

Solution

In order to separate the invoice items, we will load an XmlDocument with the invoice XML from the Invoice.xml file:

<?xml version="1.0" encoding="UTF-8"?> <Invoice invoiceDate='2003-10-05' invoiceNumber='INV-01'> <shipInfo> <name>Beerly Standing</name> <attn>Receiving</attn> <street>47 South Street</street> <city>Intox</city> <state>NH</state> </shipInfo> <billInfo> <name>Beerly Standing</name> <attn>Accounting</attn> <street>98 North Street</street> <city>Intox</city> <state>NH</state> </billInfo> <Items> <item partNum="98745"> <productName>Brown Eyed Stout</productName> <quantity>12</quantity> <price>23.99</price> <shipDate>2003-12-20</shipDate> </item> <item partNum="34987"> <productName>Diamond Pearl Lager</productName> <quantity>22</quantity> <price>35.98</price> <shipDate>2003-12-20</shipDate> </item> <item partNum="AK254"> <productName>Job Site Ale</productName> <quantity>50</quantity> <price>12.56</price> <shipDate>2003-11-12</shipDate> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required