生成XML时给节点元素加属性的问题
doc.LoadXml( "<?xml version='1.0' ?><root />" );
XmlDocument doc = new XmlDocument();
doc[ "root" ].AppendChild( doc.CreateElement( "table" ) );
doc[ "root" ]["table"].AppendChild( doc.CreateElement( "tr" ) );
doc.Save("Resultout.Xml");
这样将会生成
<?xml version="1.0"?>
<root>
<table />
<tr />
</root>
现在我想给table ,tr元素加上属性和属性值
,也就是上面的程序改成生成以下程序我怎么做呢?谢谢大家!如这样
<?xml version="1.0"?>
<root>
<table border=1 />
<tr height=30 />
</root>
问题点数:50、回复次数:3Top
1 楼hujiiori(Coder×Coder——sytu)回复于 2005-02-22 15:48:55 得分 46
先找到table节点,假设叫tableNode
tableNode.Attributes.Append(doc.CreateAttribute("border"));
tableNode.Attributes["border"].Value="1";Top
2 楼doubon(DB.NET)回复于 2005-02-22 15:51:01 得分 2
顶!Top
3 楼web_gus(树欲静而风不止)回复于 2005-02-22 15:51:45 得分 2
XmlElement.SetAttribute 方法 [C#]请参见
[C#]
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>");
XmlElement root = doc.DocumentElement;
// Add a new attribute.
root.SetAttribute("genre", "urn:samples", "novel");
Console.WriteLine("Display the modified XML...");
Console.WriteLine(doc.InnerXml);
}
}Top




