[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Urgent Help on XmlDocument

Posted on 2004-10-20
5
Medium Priority
?
672 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

649 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