yymae
asked on
Urgent Help on XmlDocument
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.CreateXmlDeclaratio n("1.0","u tf-8",null );
XmlElement rootNode = xmlDoc.CreateElement("Cust omers");
xmlDoc.InsertBefore(xmlDec laration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNod e);
XmlDocument SubNodes = new XmlDocument ();
string strStruc = "<CustData><CustName></Cus tName><Des c></Desc>< /CustData> ";
SubNodes.LoadXml(strStruc) ;
XmlNodeList XmlRootNode = xmlDoc.GetElementsByTagNam e("Custome rs");
XmlNodeList docSubNode = SubNodes.GetElementsByTagN ame("CustD ata");
XmlNodeList CustNameNode = xmlDoc.GetElementsByTagNam e("CustNam e");
XmlNodeList DescNode = xmlDoc.GetElementsByTagNam e("Desc");
//Loop RecordSet
if (colPE.Count > 0 )
{
for (int i = 0; i < colPE.Count; i++)
{
XmlRootNode.Item(0).Append Child(xmlD oc.ImportN ode(docSub Node .Item(0).Clone() ,true)) ;
CustNameNode.Item(i).Inner Text = colPE.Item(i).CustName.ToS tring().Tr im();
DescNode.Item(i).InnerText = colPE.Item(i).Desc.ToStrin g();
}
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</TelNu mber>
</TelephoneData>
<TelephoneData>
<TelDesc>Home</TelDesc>
<TelNumber>12345678</TelNu mber>
</TelephoneData>
</CustData>
<CustData>
<CustName>789</CustName>
<Desc>TESTING 123</Desc>
<TelephoneData>
<TelDesc>Home</TelDesc>
<TelNumber>456487987</TelN umber>
</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
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.CreateXmlDeclaratio
XmlElement rootNode = xmlDoc.CreateElement("Cust
xmlDoc.InsertBefore(xmlDec
xmlDoc.AppendChild(rootNod
XmlDocument SubNodes = new XmlDocument ();
string strStruc = "<CustData><CustName></Cus
SubNodes.LoadXml(strStruc)
XmlNodeList XmlRootNode = xmlDoc.GetElementsByTagNam
XmlNodeList docSubNode = SubNodes.GetElementsByTagN
XmlNodeList CustNameNode = xmlDoc.GetElementsByTagNam
XmlNodeList DescNode = xmlDoc.GetElementsByTagNam
//Loop RecordSet
if (colPE.Count > 0 )
{
for (int i = 0; i < colPE.Count; i++)
{
XmlRootNode.Item(0).Append
CustNameNode.Item(i).Inner
DescNode.Item(i).InnerText
}
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</TelNu
</TelephoneData>
<TelephoneData>
<TelDesc>Home</TelDesc>
<TelNumber>12345678</TelNu
</TelephoneData>
</CustData>
<CustData>
<CustName>789</CustName>
<Desc>TESTING 123</Desc>
<TelephoneData>
<TelDesc>Home</TelDesc>
<TelNumber>456487987</TelN
</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
ASKER
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</Te lDescripti on>
<TelNumber>12345678</TelNu mber>
</TelephoneData>
<TelephoneData> <!-- Loop Telephone Data 2 -->
<TelDescription>Home</TelD escription >
<TelNumber>12345678</TelNu mber>
</TelephoneData>
</CustData>
</Customers>
After I tried your example it will return as below :-
XmlNodeList nodeList = xmlDoc.SelectNodes("//Cust omers/Cust Data");
foreach(XmlNode node in nodeList)
{
XmlElement xmlElementPhone = xmlDoc.CreateElement("Tele phoneData" );
node.AppendChild(xmlElemen tPhone);
XmlElement xmlElementDesc = xmlDoc.CreateElement("TelD escription ");
XmlText xmlTextDesc = xmlDoc.CreateTextNode("Som e telephone Desc");
xmlElementPhone.AppendChil d(xmlTextD esc);
node.AppendChild(xmlElemen tDesc);
}
<Customers>
<CustData>
<CustName>123</CustName>
<Desc>TESTING</Desc>
<TelephoneData />
<TelDescription>Office</Te lDescripti on>
<TelNumber>12345678</TelNu mber>
<TelephoneData />
<TelephoneData />
<TelDescription>Home</TelD escription >
<TelNumber>32423423</TelNu mber>
<TelephoneData />
</CustData>
</Customers>
Hope you can provide solutions on this.
Thanks in advance.
Cheers,
yymae
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</Te
<TelNumber>12345678</TelNu
</TelephoneData>
<TelephoneData> <!-- Loop Telephone Data 2 -->
<TelDescription>Home</TelD
<TelNumber>12345678</TelNu
</TelephoneData>
</CustData>
</Customers>
After I tried your example it will return as below :-
XmlNodeList nodeList = xmlDoc.SelectNodes("//Cust
foreach(XmlNode node in nodeList)
{
XmlElement xmlElementPhone = xmlDoc.CreateElement("Tele
node.AppendChild(xmlElemen
XmlElement xmlElementDesc = xmlDoc.CreateElement("TelD
XmlText xmlTextDesc = xmlDoc.CreateTextNode("Som
xmlElementPhone.AppendChil
node.AppendChild(xmlElemen
}
<Customers>
<CustData>
<CustName>123</CustName>
<Desc>TESTING</Desc>
<TelephoneData />
<TelDescription>Office</Te
<TelNumber>12345678</TelNu
<TelephoneData />
<TelephoneData />
<TelDescription>Home</TelD
<TelNumber>32423423</TelNu
<TelephoneData />
</CustData>
</Customers>
Hope you can provide solutions on this.
Thanks in advance.
Cheers,
yymae
Ok I understand. Do you have the complete Telephone in an xmldocument or node? Like:
<TelephoneData>
<TelDesc>Office</TelDesc>
<TelNumber>12345678</TelNu mber>
</TelephoneData>
And you want to know how to add it to the customer xml? If so, use:
XmlNodeList nodeList = xmlDoc.SelectNodes("//Cust omers/Cust Data");
foreach(XmlNode node in nodeList)
{
XmlNode clonedXml = xmlDoc.ImportNode(<the TelephoneData Node>, true);
node.AppendChilde(clonedXm l);
}
-------------------------- ------
<the TelephoneData Node> should be replaced by the <TelephoneData> Node in your other xml section.
HTH,
Razzie
<TelephoneData>
<TelDesc>Office</TelDesc>
<TelNumber>12345678</TelNu
</TelephoneData>
And you want to know how to add it to the customer xml? If so, use:
XmlNodeList nodeList = xmlDoc.SelectNodes("//Cust
foreach(XmlNode node in nodeList)
{
XmlNode clonedXml = xmlDoc.ImportNode(<the TelephoneData Node>, true);
node.AppendChilde(clonedXm
}
--------------------------
<the TelephoneData Node> should be replaced by the <TelephoneData> Node in your other xml section.
HTH,
Razzie
ASKER
hi Razzie,
How to define the complete Telephone in an xmldocument or node ?
<TelephoneData>
<TelDesc>Office</TelDesc>
<TelNumber>12345678</TelNu mber>
</TelephoneData>
Cheers,
yymae
How to define the complete Telephone in an xmldocument or node ?
<TelephoneData>
<TelDesc>Office</TelDesc>
<TelNumber>12345678</TelNu
</TelephoneData>
Cheers,
yymae
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
XmlNodeList nodeList = xmlDoc.SelectNodes("//Cust
foreach(XmlNode node in nodeList)
{
XmlElement xmlElementPhone = xmlDoc.CreateElement("Tele
XmlText xmlTextPhone = xmlDoc.CreateTextNode("Som
xmlElementPhone.AppendChil
node.AppendChild(xmlElemen
}
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