C# 生成 XML
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 = "我在这战斗的一年里"; bookTitle.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>