Click here to Skip to main content
Licence CPOL
First Posted 31 Oct 2005
Views 24,611
Downloads 131
Bookmarked 13 times

Tearing Apart an XML Document

By | 31 Oct 2005 | Article
You have an XML document that needs to be broken apart into its constituent parts.

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:

<?xml version="1.0" encoding="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( );
 
// pick up MyDoc from deposited directory
xmlDoc.Load(@"..\..\MyDoc.xml");
 
// get the MyDoc element node
XmlNode MyDoc = xmlDoc.SelectSingleNode("/MyDoc");

// get the MyDoc date attribute
XmlAttribute invDate = 
  (XmlAttribute)MyDoc.Attributes.GetNamedItem("Date");
 
// get the MyDoc number attribute
XmlAttribute invNum = 
  (XmlAttribute)MyDoc.Attributes.GetNamedItem("Designer");

#endregion

#region tear apart <Test>

// Process the <Test> information to Accounting
XmlElement Test = xmlDoc.CreateElement("Test");

// correlate this information back to the original MyDoc number and date
Test.Attributes.Append((XmlAttribute)invDate.Clone( ));
Test.Attributes.Append((XmlAttribute)invNum.Clone( ));
XmlNodeList TestList = xmlDoc.SelectNodes("/MyDoc/Test");
 
// add the <Test> information to the document
foreach(XmlNode TestInfo in TestList)
{
  Test.AppendChild(TestInfo.Clone( ));
}
Console.WriteLine("Test:\r\n{0}",Test.OuterXml);
 
// Save a copy of the document
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>
 
// Process the <BookList> information to Accounting
XmlElement BookList = xmlDoc.CreateElement("BookList");
 
// correlate this information back to the original MyDoc number and date
BookList.Attributes.Append((XmlAttribute)invDate.Clone( ));
BookList.Attributes.Append((XmlAttribute)invNum.Clone( ));
XmlNodeList Booklist = xmlDoc.SelectNodes("/MyDoc/BookList");
 
// add the <BookList> information to the document
foreach(XmlNode BookListInfo in Booklist)
{
  BookList.AppendChild(BookListInfo.Clone( ));
}
Console.WriteLine("BookList:\r\n{0}",BookList.OuterXml);
 
// Save a copy of the document
fileStream = File.Create(@"..\..\BookList.xml");
bytes = Encoding.ASCII.GetBytes(BookList.OuterXml);
fileStream.Write(bytes,0,bytes.Length);
fileStream.Close( );

#endregion
 
#region tear apart <PhoneBook>
 
// Process the item information to <PhoneBook>
XmlElement PhoneBook = xmlDoc.CreateElement("PhoneBook");
 
// correlate this information back to the original MyDoc number and date
PhoneBook.Attributes.Append((XmlAttribute)invDate.Clone( ));
PhoneBook.Attributes.Append((XmlAttribute)invNum.Clone( ));
XmlNodeList itemList = xmlDoc.SelectNodes("/MyDoc/PhoneBook/No");
 
// add the item information to the document
foreach(XmlNode item in itemList)
{
  PhoneBook.AppendChild(item.Clone( ));
}
Console.WriteLine("PhoneBook:\r\n{0}",PhoneBook.OuterXml);
 
// Save a copy of the document
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>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

ShahabFatemi

Other

Sweden Sweden

Member

I got double MSc degree in Space Science and Technology from Luleå Technical University in Sweden and Paul Sabatier Toulouse in France.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 Pinmembercool_cupid_jny0:25 5 Jan '11  
GeneralMy vote of 1 Pinmembervital3d9:42 24 Apr '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 31 Oct 2005
Article Copyright 2005 by ShahabFatemi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid