【问题标题】:How can I create this XML in .Net?如何在 .Net 中创建此 XML?
【发布时间】:2012-03-09 11:07:09
【问题描述】:

我需要在 .Net 中将一个对象序列化为以下 XML。

可能最简单的方法是实现 IXMLSerializable,因为我需要对最终结果进行控制......我需要做什么才能在以下模式中输出:

<ns2:ProcessRepairOrder languageCode="de-DE" releaseID="1.0" systemEnvironmentCode="PROD" versionID="1.0">
    <ns2:ApplicationArea>
       <ns2:Sender>
          <ns2:CreatorNameCode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns3:TextType">SomeVendor</ns2:CreatorNameCode>
          <ns2:SenderNameCode name="Dave"/>
       <ns2:Sender>
    </ns2:ApplicationArea>
</ns2:ProcessRepairOrder>

更具体地说,它是没有命名空间的前缀,以及我无法进入 XML 的 xsi:type。

完整版如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
    <ns1:PutMessage xmlns:ns1="http://www.starstandards.org/webservices/2009/transport" xmlns:ns2="http://www.starstandard.org/STAR/5" xmlns:ns3="http://www.openapplications.org/oagis/9">
      <ns1:payload>
        <ns1:content>
          <ns2:ProcessRepairOrder languageCode="de-DE" releaseID="1.0" systemEnvironmentCode="PROD" versionID="1.0">
          ...many more XML elements
      </ns2:ProcessRepairOrder>
    </ns1:PutMessage>
  </S:Body>
</S:Envelope>

【问题讨论】:

  • 我能想到的一切——这包括实现 IXMLSerializable 和在内部使用 XMLDocument。我不知道如何制作上述内容。我可以粘贴示例,但它们只会使问题变得混乱,因为我有大约 50 行代码不会产生所需的内容......
  • 你在实现 IXMLSerializable 的时候发生了什么,有什么不满意的地方?
  • 您是否尝试将 *.xsd 文件输入 xsd.exe,生成一个类并将其用于序列化?
  • @AVIDeveloper 我们没有得到 xsd 文件。
  • @TomW 这并不令人满意,因为我无法让它生成相同的 XML。下面的 XML 答案链接看起来是正确的方向。

标签: .net xml xml-serialization xmldocument xmlwriter


【解决方案1】:

您可以使用 .NET 3.5 吗?如果是这样,LINQ-to-XML 可能更容易使用。这是我测试过的一些代码,它们会在您的问题中准确生成 XML 输出:

XNamespace ns2 = "some-uri";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";

XElement root = new XElement("Root",
    // Define ns2 namespace on Root element
    new XAttribute(XNamespace.Xmlns + "ns2", ns2.NamespaceName),
    new XElement(ns2 + "ProcessRepairOrder",
        // Attributes of ProcessRepairOrder
        new XAttribute("languageCode", "de-DE"),
        new XAttribute("releaseID", "1.0"),
        new XAttribute("systemEnvironmentCode", "PROD"),
        new XAttribute("versionID", "1.0"),
        // Child elements of ProcessRepairOrder
        new XElement(ns2 + "ApplicationArea",
            new XElement(ns2 + "Sender",
                new XElement(ns2 + "CreatorNameCode",
                    // Attributes of CreatorNameCode and define xsi namespace
                    new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
                    new XAttribute(xsi + "type", "ns3:TextType"),
                    // Value of CreatorNameCode,
                    "SomeVendor"
                ),
                new XElement(ns2 + "SenderNameCode",
                    // Attributes of SenderNameCode
                    new XAttribute("name", "Dave")
                )
            )
        )
    )
);

您可以查看this MSDN page 了解如何使用 LINQ-to-XML 创建 XML 树,this link 解释如何将 XML 命名空间应用于它们。

已编辑:

从下面的 cmets 开始,您似乎需要一种不太标准的方法,因为您不能拥有我上面定义的根元素,但 LINQ-to-XML 似乎需要它才能创建有效的定义了正确命名空间的 XML 结构。如果你想使用这种方法,那么有一种方法可以绕过它,但它需要更多地摆弄输出。

使用我上面编写的代码然后执行root.Elements().First().ToString() 将生成这个序列化的 XML:

<ns2:ProcessRepairOrder languageCode="de-DE" releaseID="1.0" systemEnvironmentCode="PROD" versionID="1.0" xmlns:ns2="some-uri">
  <ns2:ApplicationArea>
    <ns2:Sender>
      <ns2:CreatorNameCode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns3:TextType">SomeVendor</ns2:CreatorNameCode>
      <ns2:SenderNameCode name="Dave" />
    </ns2:Sender>
  </ns2:ApplicationArea>
</ns2:ProcessRepairOrder>

几乎在那里,但请注意 LINQ-to-XML 已在 ProcessRepairOrder 上插入了 xmlns:ns2 命名空间,因为要在整个层次结构中使用 ns2 元素,需要在某处定义它。但是现在您有了字符串格式的内容,您可以轻松地使用String.Replace() 删除它,您最终会得到您需要的内容。

我不会说这是一个优雅的解决方案,但它适用于您生成 XML 的非标准方法。

【讨论】:

  • 看起来很有希望,我会创建一个测试来看看。
  • 您添加了一个不允许的根元素 - 根元素必须是“
  • 我可以在其中一个链接上看到一些类似的示例 - msdn.microsoft.com/en-us/library/bb387075.aspx,但我需要创建 而不是 adventure-works.com">
  • 这听起来不像标准的 XML - 你肯定必须在某处定义 ns2 的命名空间吗?
  • 老实说,这可能令人头疼 - 我已经更新了问题以包括围绕我已经发布的位的其余 XML。我还不能创建服务代理代码,服务不会构建,但基本上有一个内容对象,我需要将 xml 放入其中。这对你有意义吗?
猜你喜欢
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多