我试图在C#中根据客户端的请求来格式化XML页面。下面是我目前在XML创建在.NET中编写XML
<?xml version="1.0" encoding="utf-8"?>
<!--This is to write the connection strings, text file location, and report destination.-->
<AdminPaths Name="sqlConnection1" connectionString="asdf">
<TextPath>
<Key Value="Test3" xmlns="Path" />
</TextPath>
</AdminPaths>
这是我想拥有的一切:
<?xml version="1.0" encoding="utf-8"?>
<!--This is to write the connection string, text file location, and report destination.-->
<AdminPaths>
<Name="sqlConnection1" connectionString="asdf">
</AdminPaths>
<TextPath>
< Key="Path" Value="Test3">
< Key="Report" Value="Test2">
</TextPath>
这是我目前使用的代码:
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create("C:\\Users\\fthompson11\\WebFile.xml", settings);
writer.WriteStartDocument();
writer.WriteComment("This is to write the connection strings, text file location, and report destination.");
writer.WriteStartElement("AdminPaths");
writer.WriteAttributeString("Name", "sqlConnection1");
writer.WriteAttributeString("connectionString", "asdf");
writer.WriteStartElement("TextPath");
writer.WriteStartElement("Key", "Path");
writer.WriteAttributeString("Value", "Test3");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
我曾尝试使用writer.WriteEndElement();在writeAttributeString启动之后,但我收到一个错误,指出“如果要编写XML片段,请确保ConformanceLevel设置设置为ConformanceLevel.Fragment或ConformanceLevel.Auto。”我相信这不是我想要做的?任何帮助,将不胜感激。
谢谢。
“我想吃点什么”的模样无效的XML ... – millimoose 2013-04-10 22:28:41
什么是不正确的呢? – 2013-04-10 22:29:17
@BigPoppa xml应该有单根元素 – 2013-04-10 22:30:22