Add to an XML node

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wcmcalister@gmail.com

    Add to an XML node

    I have a web service that returns me an XmlNode ojbect. Here is an
    example of the outerXML:
    <?xml version="1.0" encoding="utf-8" ?>
    <NewDataSet>
    <Table>
    <ID>123</ID>
    <TheName>Bob<Th eName>
    </Table
    <Table>
    <ID>124</ID>
    <TheName>Barry< TheName>
    </Table
    <Table>
    <ID>125</ID>
    <TheName>Beth<T heName>
    </Table
    <Table>
    <ID>126</ID>
    <TheName>Beatri x<TheName>
    </Table
    <Table>
    <ID>127</ID>
    <TheName>Benjam in<TheName>
    </Table
    <Table>
    <ID>128</ID>
    <TheName>Betty< TheName>
    </Table
    </NewDataSet>

    I also have a list of IDs with some AccountNumbers that looks like
    this:
    ID AccountNumber
    123 987654321
    124 987654322
    125 987654323
    126 987654324
    127 987654325
    128 987654326

    I would like to match this info into the XML so that it looks like
    this:
    <?xml version="1.0" encoding="utf-8" ?>
    <NewDataSet>
    <Table>
    <ID>123</ID>
    <TheName>Bob<Th eName>
    <AccountNum>987 654321</AccountNum>
    </Table
    <Table>
    <ID>124</ID>
    <TheName>Barry< TheName>
    <AccountNum>987 654322</AccountNum>
    </Table
    <Table>
    <ID>125</ID>
    <TheName>Beth<T heName>
    <AccountNum>987 654323</AccountNum>
    </Table
    <Table>
    <ID>126</ID>
    <TheName>Beatri x<TheName>
    <AccountNum>987 654324</AccountNum>
    </Table
    <Table>
    <ID>127</ID>
    <TheName>Benjam in<TheName>
    <AccountNum>987 654325</AccountNum>
    </Table
    <Table>
    <ID>128</ID>
    <TheName>Betty< TheName>
    <AccountNum>987 654326</AccountNum>
    </Table
    </NewDataSet>

    Can someone show me how to match the data into the XmlNode object?

    Thanks!
  • Martin Honnen

    #2
    Re: Add to an XML node

    wcmcalister@gma il.com wrote:
    Can someone show me how to match the data into the XmlNode object?
    Use XPath to find the right Table element, then use CreateElement to
    create a new AccountNum element, then use AppendChild to add the newly
    created element. When finished, save the XML document:

    XmlDocument doc = new XmlDocument();
    doc.Load(@"..\. .\XMLFile1.xml" );

    List<Dataaccoun ts = new List<Data>();
    Data account = new Data();
    account.ID = 123;
    account.Account Number = 987654321;
    accounts.Add(ac count);
    account = new Data();
    account.ID = 124;
    account.Account Number = 987654322;
    accounts.Add(ac count);

    foreach (Data data in accounts)
    {
    XmlNode table =
    doc.SelectSingl eNode(string.Fo rmat("/NewDataSet/Table[ID = {0}]", data.ID));
    if (table != null)
    {
    XmlElement accountNum =
    doc.CreateEleme nt("AccountNum" );
    accountNum.Inne rText = data.AccountNum ber.ToString();
    table.AppendChi ld(accountNum);
    }
    }
    //saving to Console.Out for testing, could save to file or
    stream instead
    doc.Save(Consol e.Out);



    --

    Martin Honnen --- MVP XML

    Comment

    • =?Utf-8?B?d2NtY2FsaXN0ZXI=?=

      #3
      Re: Add to an XML node

      Thanks so much Martin. I can now finish my project on time.

      I don't absolutely need this but is there an easy way to convert the
      XmlDocument back to an XmlNode?

      Comment

      • Martin Honnen

        #4
        Re: Add to an XML node

        wcmcalister wrote:
        Thanks so much Martin. I can now finish my project on time.
        >
        I don't absolutely need this but is there an easy way to convert the
        XmlDocument back to an XmlNode?
        XmlDocument is a subclass of XmlNode so any XmlDocument instance is also
        an instance of XmlNode. So there is no need to do any conversion.

        --

        Martin Honnen --- MVP XML

        Comment

        • =?Utf-8?B?d2NtY2FsaXN0ZXI=?=

          #5
          Re: Add to an XML node

          Thank you Martin. I have a lot learn about working with XML in dot Net.

          Have a wonderful day (I know I am)!

          Comment

          Working...