【问题标题】:Writing XML In .NET在 .NET 中编写 XML
【发布时间】:2013-04-10 22:27:50
【问题描述】:

我正在尝试根据客户端的请求在 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。”我相信那不是我想做的事?任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • “我想要的”看起来像无效的 XML...
  • 这有什么不妥之处?
  • @BigPoppa xml 应该有单个根元素
  • @lazyberezovsky 通过单个根元素,你的意思是我只需要 吗?
  • 不起作用。您正在描述一个具有 2 个属性的无名元素。

标签: c# xml asp.net-mvc xmlwriter


【解决方案1】:

认为这是您想要的 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.FragmentConformanceLevel.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();

【讨论】:

  • 再次感谢。这很有帮助。
【解决方案2】:

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

  • 不止一个根元素,即围绕所有内容的单个&lt;&gt; 元素。在第一个示例中,&lt;AdminPaths&gt; 是单个根元素,因为其他所有内容都包含在其中
  • 没有名称的节点。例如,&lt;add="" 无效 - 如果节点的名称是 add,那么它不能直接有值。方法是&lt;add&gt;Value&lt;/add&gt;。否则,您必须具有&lt;add key=""&gt; 之类的属性
  • 同样,&lt; Key="Path" Value="Test3"&gt; 是无效的,因为它没有名称,只有两个属性(键和值),而且它没有关闭(节点需要有一个像 &lt;add&gt;&lt;/add&gt; 这样的结束标签或者是自动关闭的比如&lt;add /&gt;(注意&gt;前面的/)

阅读 XML 中 Node 或 Element 与 Attribute 之间的区别,应该会比较清楚您需要构建什么,然后我们可以解决如何构建。

【讨论】:

  • 我认为这应该是一个评论,而不是一个解决方案
  • @lazyberezovsky 评论太长了:/
  • 同意,但从技术上讲,这仍然是一条评论 :)
  • 我认为这是一个有效的帖子,只是因为@MichaelStum 指出了代码的哪些部分需要处理。
  • 同意。我没有足够的代表投票,否则我会投票。谢谢@MichaelStum
猜你喜欢
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 2010-12-13
  • 1970-01-01
  • 2017-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多