|
-
Apr 5th, 2003, 05:45 PM
#1
Thread Starter
Hyperactive Member
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)

-
Apr 5th, 2003, 05:56 PM
#2
PowerPoster
Here you go:
Code:
doc.DocumentElement.SetAttribute("id", "14");
or...
Code:
XmlAttribute attr = doc.CreateAttribute("id");
attr.Value = "14";
doc.DocumentElement.SetAttributeNode(attr);
-
Apr 5th, 2003, 06:10 PM
#3
Thread Starter
Hyperactive Member
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)

-
Apr 5th, 2003, 06:19 PM
#4
PowerPoster
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);
-
Apr 5th, 2003, 06:25 PM
#5
Thread Starter
Hyperactive Member
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)

-
Apr 5th, 2003, 06:29 PM
#6
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|