写入xml文件时的错误
yjk 2004-08-06 12:58:12 从程序中将数据写入一个xml的文件,发现不是我想要得格式,该怎样写呢。
代码如下:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(XmlPath);
XmlElement PostElement = xmlDoc.CreateElement("Resource");
XmlElement Title = xmlDoc.CreateElement("Title",postToAdd.Subject);
XmlElement Creator = xmlDoc.CreateElement("Creator",postToAdd.Username);
XmlElement Type = xmlDoc.CreateElement("Type","Post");
XmlElement Identifier = xmlDoc.CreateElement("Identifier", newPost.ThreadID);
PostElement.AppendChild(Title);
PostElement.AppendChild(Creator);
PostElement.AppendChild(Type);
PostElement.AppendChild(Identifier);
//get the root element of the xml file
XmlElement root = xmlDoc.DocumentElement;
//add the PostElement as the child of root element
root.AppendChild(PostElement);
xmlDoc.Save(OutPath);
生成的结果如下
<?xml version="1.0" encoding="gb2312"?>
<Resources>
<Resource>
<Title xmlns="执子之手,与子偕老" />
<Creator xmlns="admin" />
<Type xmlns="Post" />
<Identifier xmlns="http://localhost/Forum/ShowPost.aspx?PostID=266" />
</Resource>
<Resources>
但我想要得结果是
<?xml version="1.0" encoding="gb2312"?>
<Resources>
<Resource>
<Title>执子之手,与子偕老</Title>
<Creator>admin</Creator>
<Type>Post</Type>
<Identifier>http://localhost/ShowPost.aspx?PostID=266</Identifier>
</Resource>
<Resources>
该怎么输出呢?
谢谢