Link to home
Start Free TrialLog in
Avatar of yymae
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.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
Avatar of Razzie_
Razzie_

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
Avatar of 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</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
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


Avatar of yymae

ASKER

hi Razzie,

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

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


Cheers,
yymae
ASKER CERTIFIED SOLUTION
Avatar of Razzie_
Razzie_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial