15.9. Tearing Apart an XML Document
Problem
You have an XML document that needs to be broken apart into multiple 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, load an XElement
with the invoice XML from the Invoice.xml file shown in Example 15-7.
Example 15-7. Invoice.xml
<?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> ...
Get C# 3.0 Cookbook, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.