C# 在.NET中编写XML

C# 在.NET中编写XML,c#,xml,asp.net-mvc,xmlwriter,C#,Xml,Asp.net Mvc,Xmlwriter,我正试图根据客户机的请求,用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">

我正试图根据客户机的请求,用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>
我已尝试使用writer.WriteEndElement();在writeAttributeString启动后,我收到一个错误,该错误表示“如果要编写XML片段,请确保ConformanceLevel设置设置为ConformanceLevel.Fragment或ConformanceLevel.Auto。”我相信这不是我想要做的事情?任何帮助都将不胜感激


谢谢。

这是无效的XML,原因如下:

  • 多个根元素,即围绕所有内容的单个
    元素。在第一个示例中,
    是单个根元素,因为其他所有元素都包含在其中
  • 没有名称的节点。例如,
    是无效的,因为它没有名称,只有两个属性(键和值),而且它没有关闭(节点需要有一个像
    一样的关闭标记,或者像
    一样自动关闭(请注意
    之前的/)

阅读XML中节点或元素与属性之间的差异,应该会比较清楚需要构建什么,然后我们可以解决如何构建。这是无效的XML,原因如下:

  • 多个根元素,即围绕所有内容的单个
    元素。在第一个示例中,
    是单个根元素,因为它包含所有其他元素
  • 没有名称的节点。例如,
    无效,因为它没有名称,只有两个属性(键和值),而且它没有关闭(节点需要有一个关闭标记,如
    ,或者是自动关闭标记,如
    (注意
    之前的/)
阅读XML中节点或元素与属性之间的差异,应该会比较清楚需要构建什么,然后我们可以解决如何构建的问题。

我认为这就是您想要的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格式不正确

<?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…有什么不正确的地方吗?@BigPoppaXML应该有一个根element@lazyberezovsky你是说我只需要有一个根元素吗?这个行不通。你描述的是一个有两个属性的无名元素。“我想要什么”看起来像是无效的XML…有什么不正确的地方吗?@BigPoppa XML应该有一个根element@lazyberezovsky单根元素,你的意思是我只需要有一个?这个行不通。你描述的是一个有两个属性的无名元素。我认为这应该是一个注释,而不是一个注释solution@lazyberezovsky这太长了,不能作为评论:/Agree,但是从技术上讲,这仍然是一个评论:)我认为这是一篇有效的帖子,如果只是因为@MichaelStum指出代码的哪些部分需要改进的话。同意。我没有足够的代表投票支持,否则我会投票支持。谢谢@MichaelStumI,我认为这应该是一篇评论,而不是一篇评论solution@lazyberezovsky评论太长了:/同意,但从技术上讲,这仍然是一个评论:)我认为这是一篇有效的帖子,只要@MichaelStum指出代码的哪些部分需要处理就行了。我没有足够的代表投票,否则我会。谢谢@Michaelstum再次感谢。这帮了大忙,再次谢谢。这很有用。
<?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>
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();