//建一個新的空的XML文檔
XmlTextWriter objXml = new XmlTextWriter(Server.MapPath("Text.xml"),null);
//格式化輸出XML文檔
objXml.Formatting = Formatting.Indented;
objXml.Indentation = 4;
//寫入XML文檔標記
objXml.WriteStartDocument();
//寫入XML文檔注釋
objXml.WriteComment("Created using an XML" + Context.Timestamp);
//寫入根元素
objXml.WriteStartElement("BookList");
//寫入元素
objXml.WriteStartElement("Book");
//寫入屬性
objXml.WriteAttributeString("Category","Technology");
//寫入屬性值
objXml.WriteAttributeString("PageCount","1234");
//寫入子元素及文本值
objXml.WriteElementString("Title","Professional Video Recorder Programming");
//寫入子元素及文本值
objXml.WriteElementString("ReleaseDate","02/02/2002");
//寫入元素
objXml.WriteStartElement("AuthorList");
//寫入子元素及文本值
objXml.WriteElementString("Author","Framcesca Unix");
//寫入子元素及文本值
objXml.WriteElementString("Author","william Soft");
//關閉子元素、元素、根元素
objXml.WriteEndElement();
objXml.WriteEndElement();
objXml.WriteEndElement();
//清除緩存
objXml.Flush();
//關閉對像
objXml.Close();
博客展示了使用C#创建新的XML文档的代码示例。包括创建空文档、格式化输出、写入标记、注释、元素、属性及文本值等操作,最后清除缓存并关闭对象,涉及XML文档的基本构建流程。
698

被折叠的 条评论
为什么被折叠?



