?
Solved

Urgent Help on XmlDocument

Posted on 2004-10-20
5
Medium Priority
?
667 Views
Last Modified: 2008-03-04
Dear all,

I'm newbie to XML in C#. Below it's the existing XMLDocument I need to generate using System.Xml. The <CustData> will loop thru a recordset to generate it.

Example Codes :-

XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);
XmlElement rootNode  = xmlDoc.CreateElement("Customers");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNode);

XmlDocument SubNodes = new XmlDocument ();
string strStruc = "<CustData><CustName></CustName><Desc></Desc></CustData>";
SubNodes.LoadXml(strStruc);

XmlNodeList XmlRootNode = xmlDoc.GetElementsByTagName("Customers");
XmlNodeList docSubNode = SubNodes.GetElementsByTagName("CustData");
XmlNodeList CustNameNode = xmlDoc.GetElementsByTagName("CustName");
XmlNodeList DescNode = xmlDoc.GetElementsByTagName("Desc");

//Loop RecordSet
if (colPE.Count > 0 )      
{
  for (int i = 0; i < colPE.Count; i++)
  {
    XmlRootNode.Item(0).AppendChild(xmlDoc.ImportNode(docSubNode .Item(0).Clone() ,true)) ;
    CustNameNode.Item(i).InnerText = colPE.Item(i).CustName.ToString().Trim();
    DescNode.Item(i).InnerText = colPE.Item(i).Desc.ToString();
  }
  SubNodes = null;
}

Example XMLDocument:-

<Customers>
    <CustData>
      <CustName>123</CustName>
      <Desc>TESTING</Desc>
    </CustData>
    <CustData>
      <CustName>789</CustName>
      <Desc>TESTING 123</Desc>
    </CustData>
</Customers>

Further enhance the XmlDocument, I need to loop thru <CustData> and add in the <TelephoneData> in the tag. I need to loop thru another recordset to get <TelephoneData> for each customer.

Example XmlDocument :-

<Customers>
    <CustData>
      <CustName>123</CustName>
      <Desc>TESTING</Desc>
      <TelephoneData>
      <TelDesc>Office</TelDesc>
      <TelNumber>12345678</TelNumber>
      </TelephoneData>
      <TelephoneData>
      <TelDesc>Home</TelDesc>
      <TelNumber>12345678</TelNumber>
      </TelephoneData>
    </CustData>
    <CustData>
      <CustName>789</CustName>
      <Desc>TESTING 123</Desc>
      <TelephoneData>
      <TelDesc>Home</TelDesc>
      <TelNumber>456487987</TelNumber>
      </TelephoneData>
    </CustData>
</Customers>

The problem is, how to add in the additional <TelephoneData> into <CustData> ? Need example code on this.

Thank in advance.


Cheers,
yymae
0
Comment
Question by:yymae
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:Razzie_
ID: 12367655
Hmm not sure if I understand it completely, but can you use something like this:

XmlNodeList nodeList = xmlDoc.SelectNodes("//Customers/CustData");
foreach(XmlNode node in nodeList)
{
   XmlElement xmlElementPhone = xmlDoc.CreateElement("TelephoneData");
   XmlText xmlTextPhone = xmlDoc.CreateTextNode("Some telephone number");
   xmlElementPhone.AppendChild(xmlTextPhone);
   node.AppendChild(xmlElementPhone);
}

Just replace 'Some telephone number' with your actual telephone number you obtained.

Hope this helps, if you had some other problems let me know.

Razzie
0
 

Author Comment

by:yymae
ID: 12367938
Hi Razzie,

Thanks for your reply, unfortunately the example you shown me doesn't solve my problem. The XMLDocument I want is as below :-

<Customers>
    <CustData>
      <CustName>123</CustName>
      <Desc>TESTING</Desc>
      <TelephoneData> <!-- Loop Telephone Data 1 -->
            <TelDescription>Office</TelDescription>
            <TelNumber>12345678</TelNumber>
      </TelephoneData>
      <TelephoneData> <!-- Loop Telephone Data 2 -->
            <TelDescription>Home</TelDescription>
            <TelNumber>12345678</TelNumber>
      </TelephoneData>
    </CustData>
</Customers>

After I tried your example it will return as below :-


XmlNodeList nodeList = xmlDoc.SelectNodes("//Customers/CustData");
foreach(XmlNode node in nodeList)
{
   XmlElement xmlElementPhone = xmlDoc.CreateElement("TelephoneData");
   node.AppendChild(xmlElementPhone);
   
   XmlElement xmlElementDesc = xmlDoc.CreateElement("TelDescription");
   XmlText xmlTextDesc = xmlDoc.CreateTextNode("Some telephone Desc");
   xmlElementPhone.AppendChild(xmlTextDesc);
   node.AppendChild(xmlElementDesc);
}


<Customers>
    <CustData>
      <CustName>123</CustName>
      <Desc>TESTING</Desc>
      <TelephoneData />
      <TelDescription>Office</TelDescription>
      <TelNumber>12345678</TelNumber>
      <TelephoneData />
      <TelephoneData />
      <TelDescription>Home</TelDescription>
      <TelNumber>32423423</TelNumber>
      <TelephoneData />
    </CustData>
</Customers>

Hope you can provide solutions on this.

Thanks in advance.


Cheers,
yymae
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12368225
Ok I understand. Do you have the complete Telephone in an xmldocument or node? Like:

<TelephoneData>
     <TelDesc>Office</TelDesc>
     <TelNumber>12345678</TelNumber>
</TelephoneData>

And you want to know how to add it to the customer xml? If so, use:

XmlNodeList nodeList = xmlDoc.SelectNodes("//Customers/CustData");
foreach(XmlNode node in nodeList)
{
   XmlNode clonedXml = xmlDoc.ImportNode(<the TelephoneData Node>, true);
   node.AppendChilde(clonedXml);
}

--------------------------------

<the TelephoneData Node> should be replaced by the <TelephoneData> Node in your other xml section.

HTH,

Razzie


0
 

Author Comment

by:yymae
ID: 12377569
hi Razzie,

How to define the complete Telephone in an xmldocument or node ?

<TelephoneData>
     <TelDesc>Office</TelDesc>
     <TelNumber>12345678</TelNumber>
</TelephoneData>


Cheers,
yymae
0
 
LVL 8

Accepted Solution

by:
Razzie_ earned 300 total points
ID: 12385773
Well it depends, where do you get the telephone data from? from another XmlDocument? From a database? You can build such XML using:

XmlElement xmlElementPhone = xmlDoc.CreateElement("TelephoneData");

XmlElement xmlElementDesc = xmlDoc.CreateElement("TelDesc");
XmlText xmlTextDesc = xmlDoc.CreateTextNode("Office");
xmlElementNumber.AppendChild(xmlTextDesc);

XmlElement xmlElementNumber = xmlDoc.CreateElement("TelNumber");
XmlText xmlTextNumber = xmlDoc.CreateTextNode("12345678");
xmlElementNumber.AppendChild(xmlTextNumber);

xmlElementPhone.AppendChild(xmlElementDesc);
xmlElementPhone.AppendChild(xmlElementNumber);

That would create the following xml:

<TelephoneData>
     <TelDesc>Office</TelDesc>
     <TelNumber>12345678</TelNumber>
</TelephoneData>

Where you get the values like 12345678 from, I don't know.
And you can add this xml to the other xml like I did in the example of my previous post.

Cheers,

Razzie

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question