我正在尝试根据客户端的请求在C#中格式化XML页面。以下是我目前在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”。我相信这不是我想做的事情?任何帮助将不胜感激。
感谢。
答案 0 :(得分:2)
我认为这是您想要的XML。我猜,因为你声明的目标的XML没有正确形成。
<?xml version="1.0" encoding="utf-8"?>
<!--This is to write the connection strings, text file location, and report destination.-->
<AdminPaths>
<AdminPath Name="sqlConnection1" connectionString="asdf" />
<TextPath>
<Text Key="Path" Value="Test3" />
<Text Key="Report" Value="Test2" />
</TextPath>
</AdminPaths>
基本上,在你声明的目标XML中,你试图在xml文档下创建2个根节点,这是禁忌,除非你愿意使用ConformanceLevel.Fragment
或ConformanceLevel.Auto
并强迫这种行为。
此代码如下:
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.");
// the AdminPaths
writer.WriteStartElement("AdminPaths");
writer.WriteStartElement("AdminPath");
writer.WriteAttributeString("Name", "sqlConnection1");
writer.WriteAttributeString("connectionString", "asdf");
writer.WriteEndElement();
// the TextPath's
writer.WriteStartElement("TextPath");
writer.WriteStartElement("Text");
writer.WriteAttributeString("Key", "Path");
writer.WriteAttributeString("Value", "Test3");
writer.WriteEndElement();
writer.WriteStartElement("Text");
writer.WriteAttributeString("Key", "Report");
writer.WriteAttributeString("Value", "Test2");
writer.WriteEndElement();
writer.WriteEndElement(); // </AdminPaths>
writer.WriteEndDocument();
writer.Flush();
writer.Close();
答案 1 :(得分:1)
这是无效的XML,原因如下:
<>
元素。在第一个示例中,<AdminPaths>
是单个根元素,因为其他所有内容都包含在其中<add=""
无效 - 如果节点的名称为add
,则它不能直接具有值。这样做的方法是<add>Value</add>
。否则,您必须拥有<add key="">
< Key="Path" Value="Test3">
无效,因为它没有名称,只有两个属性(键和值),也没有关闭(节点需要有一个结束标记,如<add></add>
或像<add />
一样自我关闭(注意>
之前的阅读节点或元素与XML中的属性之间的区别,它应该变得相对清晰,您需要构建什么,然后我们可以解决方法。