C# 生成 XML
时间:2010-09-28 来源:大豆男生
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(dec);
XmlElement root = doc.CreateElement("BookInfo");
doc.AppendChild(root);
XmlElement isbn = doc.CreateElement("ISBN");
isbn.InnerText = "6926456900364";
root.AppendChild(isbn);
XmlElement bookTitle = doc.CreateElement("BookTitle");
XmlCDataSection cd = doc.CreateCDataSection("杜拉拉3");
bookTitle.AppendChild(cd);
XmlAttribute suttitle = doc.CreateAttribute("Subtitle");
suttitle.Value = "我在这战斗的一年里";
bookName.Attributes.Append(suttitle);
root.AppendChild(bookTitle);
string xmlString = doc.OuterXml;
doc.Save(@"c:\aa.xml");
<BookInfo>
<ISBN>6926456900364</ISBN>
<BookTitle Subtitle="我在这战斗的一年里"><![CDATA[杜拉拉3]]></BookTitle>
</BookInfo>
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(dec);
XmlElement root = doc.CreateElement("BookInfo");
doc.AppendChild(root);
XmlElement isbn = doc.CreateElement("ISBN");
isbn.InnerText = "6926456900364";
root.AppendChild(isbn);
XmlElement bookTitle = doc.CreateElement("BookTitle");
XmlCDataSection cd = doc.CreateCDataSection("杜拉拉3");
bookTitle.AppendChild(cd);
XmlAttribute suttitle = doc.CreateAttribute("Subtitle");
suttitle.Value = "我在这战斗的一年里";
bookName.Attributes.Append(suttitle);
root.AppendChild(bookTitle);
string xmlString = doc.OuterXml;
doc.Save(@"c:\aa.xml");
结果:
<?xml version="1.0" encoding="UTF-8" ?><BookInfo>
<ISBN>6926456900364</ISBN>
<BookTitle Subtitle="我在这战斗的一年里"><![CDATA[杜拉拉3]]></BookTitle>
</BookInfo>
记得要 using System.Xml;
相关阅读 更多 +