By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,658 Members | 969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,658 IT Pros & Developers. It's quick & easy.

Urgent Help on XmlDocument

P: n/a
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,
Mae
Nov 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.