Introduction
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 a phonebook, an invoice, etc., in XML form.
In order to separate the XML items, we will load an XmlDocument with the invoice XML from the MyDoc.xml file:
="1.0" ="UTF-8"
<MyDoc Date='2005-10-31' Designer='Shahab Fatemi'>
<Test>
<Date>2005-10-31</Date>
<PersianDate>1384-08-09</PersianDate>
</Test>
<BookList>
<Name>C#.NET</Name>
<Author>Shahab Fatemi</Author>
<Price>49.99$</Price>
<Publisher>Naji</Publisher>
</BookList>
<PhoneBook>
<No number='1'>
<Name>Shahab</Name>
<Family>Fatemi</Family>
<Country>IRAN</Country>
<City>Esfahan</City>
<MobileNumber>0913-319-9328</MobileNumber>
<E-Mail>shahab_f_m@yahoo.com</E-Mail>
</No>
<No number='2'>
<Name>Noosha</Name>
<Family>xx</Family>
<Country>IRAN</Country>
<City>Esfahan</City>
<MobileNumber>0913-xxx-xxxx</MobileNumber>
<E-Mail>noosha@x.com</E-Mail>
</No>
<No number='3'>
<Name>Navid</Name>
<Family>Khosravi</Family>
<Country>IRAN</Country>
<City>Esfahan</City>
<MobileNumber>0913-123-4567</MobileNumber>
<E-Mail>navid@x.com</E-Mail>
</No>
<No number='4'>
<Name>Mehrdad</Name>
<Family>Saifi</Family>
<Country>IRAN</Country>
<City>Esfahan</City>
<MobileNumber>0913-123-4567</MobileNumber>
<E-Mail>kechele@kachal.com</E-Mail>
</No>
</PhoneBook>
</MyDoc>
The code to tear this document apart and send the various information pieces to their respective departments is shown here:
#region XML
XmlDocument xmlDoc = new XmlDocument( );
xmlDoc.Load(@"..\..\MyDoc.xml");
XmlNode MyDoc = xmlDoc.SelectSingleNode("/MyDoc");
XmlAttribute invDate =
(XmlAttribute)MyDoc.Attributes.GetNamedItem("Date");
XmlAttribute invNum =
(XmlAttribute)MyDoc.Attributes.GetNamedItem("Designer");
#endregion
#region tear apart <Test>
XmlElement Test = xmlDoc.CreateElement("Test");
Test.Attributes.Append((XmlAttribute)invDate.Clone( ));
Test.Attributes.Append((XmlAttribute)invNum.Clone( ));
XmlNodeList TestList = xmlDoc.SelectNodes("/MyDoc/Test");
foreach(XmlNode TestInfo in TestList)
{
Test.AppendChild(TestInfo.Clone( ));
}
Console.WriteLine("Test:\r\n{0}",Test.OuterXml);
FileStream fileStream = File.Create(@"..\..\Test.xml");
byte [] bytes = Encoding.ASCII.GetBytes(Test.OuterXml);
fileStream.Write(bytes,0,bytes.Length);
fileStream.Close( );
#endregion
#region tear apart <BookList>
XmlElement BookList = xmlDoc.CreateElement("BookList");
BookList.Attributes.Append((XmlAttribute)invDate.Clone( ));
BookList.Attributes.Append((XmlAttribute)invNum.Clone( ));
XmlNodeList Booklist = xmlDoc.SelectNodes("/MyDoc/BookList");
foreach(XmlNode BookListInfo in Booklist)
{
BookList.AppendChild(BookListInfo.Clone( ));
}
Console.WriteLine("BookList:\r\n{0}",BookList.OuterXml);
fileStream = File.Create(@"..\..\BookList.xml");
bytes = Encoding.ASCII.GetBytes(BookList.OuterXml);
fileStream.Write(bytes,0,bytes.Length);
fileStream.Close( );
#endregion
#region tear apart <PhoneBook>
XmlElement PhoneBook = xmlDoc.CreateElement("PhoneBook");
PhoneBook.Attributes.Append((XmlAttribute)invDate.Clone( ));
PhoneBook.Attributes.Append((XmlAttribute)invNum.Clone( ));
XmlNodeList itemList = xmlDoc.SelectNodes("/MyDoc/PhoneBook/No");
foreach(XmlNode item in itemList)
{
PhoneBook.AppendChild(item.Clone( ));
}
Console.WriteLine("PhoneBook:\r\n{0}",PhoneBook.OuterXml);
fileStream = File.Create(@"..\..\PhoneBook.xml");
bytes = Encoding.ASCII.GetBytes(PhoneBook.OuterXml);
fileStream.Write(bytes,0,bytes.Length);
fileStream.Close( );
#endregion
The "MyDoc" containing the various pieces of XML data for the Web Services is listed in the following sections:
Result
Test.XML
<Test Date="2005-10-31" Designer="Shahab Fatemi">
<Test>
<Date>2005-10-31</Date>
<PersianDate>1384-08-09</PersianDate>
</Test>
</Test>
BookList.XML
<BookList Date="2005-10-31" Designer="Shahab Fatemi">
<BookList>
<Name>C#.NET</Name>
<Author>Shahab Fatemi</Author>
<Price>49.99$</Price>
<Publisher>Naji</Publisher>
</BookList>
</BookList>
PhoneBook.XML
<PhoneBook>
<No number='1'>
<Name>Shahab</Name>
<Family>Fatemi</Family>
<Country>IRAN</Country>
<City>Esfahan</City>
<MobileNumber>0913-319-9328</MobileNumber>
<E-Mail>shahab_f_m@yahoo.com</E-Mail>
</No>
<No number='2'>
<Name>Noosha</Name>
<Family>xx</Family>
<Country>IRAN</Country>
<City>Esfahan</City>
<MobileNumber>0913-xxx-xxxx</MobileNumber>
<E-Mail>noosha@x.com</E-Mail>
</No>
<No number='3'>
<Name>Navid</Name>
<Family>Khosravi</Family>
<Country>IRAN</Country>
<City>Esfahan</City>
<MobileNumber>0913-123-4567</MobileNumber>
<E-Mail>navid@x.com</E-Mail>
</No>
<No number='4'>
<Name>Mehrdad</Name>
<Family>Saifi</Family>
<Country>IRAN</Country>
<City>Esfahan</City>
<MobileNumber>0913-123-4567</MobileNumber>
<E-Mail>kechele@kachal.com</E-Mail>
</No>
</PhoneBook>