Solved

Urgent Help on XmlDocument

Posted on 2004-10-20
5
653 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Image(7) 1 53
fomat Json objects 6 28
Not showing JavaScript in the list 5 36
C# Comparing String Arrays. 5 0
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

912 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

16 Experts available now in Live!

Get 1:1 Help Now