Gazishaheen’s Weblog

XML Document

Posted by: gazishaheen on: April 7, 2008

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 and 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:

Collapse
<?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:

Collapse
#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 are 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

Collapse
<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>

How to get 
protected

 object GetNodeValue(XmlDocument xmlDocumentIn, string sNodeName){

 



object oRetVal = null; 



/* Returns the list of nodes whose name is 'sNodeName' */

 

XmlNodeList oxmlNodeList = xmlDocumentIn.GetElementsByTagName(sNodeName); 



/*Looping through the xml nodes to get the first one */

 

foreach (XmlNode oxmlNode in oxmlNodeList){

oRetVal = oxmlNode.InnerXml;

 



break;}

 



return oRetVal;}



Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


  • Mr WordPress: Hi, this is a comment.To delete a comment, just log in, and view the posts' comments, there you will have the option to edit or delete them.

Categories

Follow

Get every new post delivered to your Inbox.