Results 1 to 6 of 6

Thread: Add XML Node Attribute Help..

  1. #1

    Thread Starter
    Hyperactive Member kleptos's Avatar
    Join Date
    Aug 2001
    Location
    The Dark Carnival
    Posts
    346

    Add XML Node Attribute Help..

    Below is code i have to write an a node to an xml file. How can i add an attribute to the "error" node?


    Code:
    XmlDocument doc = new XmlDocument();
    doc.Load("Errors.xml");
    
    XmlNode node = doc.CreateNode(XmlNodeType.Element,"error",null);
    XmlNode item = doc.CreateNode(XmlNodeType.Element,"date",null);
    item.InnerText = DateTime.Now.ToString();
    doc.DocumentElement.AppendChild(node);
    node.AppendChild(item);
    
    doc.Save("Errors.xml");
    The above gives me this:
    <error>
    <date>4/5/2003</date>
    </error>

    I want this:
    <error id="14">
    <date>4/5/2003</date>
    </error>
    ..::[kleptos]::..
    • Database Administrator (MSSQL 2000)
    • Application Developer (C#)
    • Web Developer (ASP.NET)


  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Here you go:

    Code:
        doc.DocumentElement.SetAttribute("id", "14");
    or...
    Code:
        XmlAttribute attr = doc.CreateAttribute("id");
        attr.Value = "14";
        doc.DocumentElement.SetAttributeNode(attr);

  3. #3

    Thread Starter
    Hyperactive Member kleptos's Avatar
    Join Date
    Aug 2001
    Location
    The Dark Carnival
    Posts
    346
    Awsome, Thank you....

    Now, my next issue, and i didnt explain it right, sorry, this is what i need it to do.

    Code:
    <errors>
      <error id="12">
        <message>Message Here</message>
      </error>
      <error id="13">
        <message>Message Here</message>
      </error>
    </errors>
    That code (which works great) gave me this
    Code:
    <errors id="12">
      <error>
        <message>Message Here</message>
      </error>
      <error>
        <message>Message Here</message>
      </error>
    </errors>
    ..::[kleptos]::..
    • Database Administrator (MSSQL 2000)
    • Application Developer (C#)
    • Web Developer (ASP.NET)


  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    All you have to do is get a reference to the node you want to append additional nodes to.

    Code:
    XmlNode node = doc.DocumentElement.SelectSingleNode("error");
    XmlAttribute x = doc.CreateAttribute("id");
    x.Value = "14";
    node.Attributes.SetNamedItem(x);

  5. #5

    Thread Starter
    Hyperactive Member kleptos's Avatar
    Join Date
    Aug 2001
    Location
    The Dark Carnival
    Posts
    346
    Thank you so much... I am trying so hard to learn C# with XML, and this helps me alot.
    ..::[kleptos]::..
    • Database Administrator (MSSQL 2000)
    • Application Developer (C#)
    • Web Developer (ASP.NET)


  6. #6
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    no problem...i have extensive experience with xml/xslt, so if you have any questions, feel free to ask.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width