Solved

Urgent Help on XmlDocument

Posted on 2004-10-20
5
651 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
  • 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 100 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ConsoleSql 1 17
dynamic menu in asp.net c# 11 27
C# DataGridView_RowsAdded event not firing 1 38
fomat Json objects 6 17
Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now