【问题标题】:Create XML file using c# like below format使用 c# 创建 XML 文件,如下格式
【发布时间】:2018-05-17 11:25:44
【问题描述】:

我需要使用以下格式的 c# 创建 xml。

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <AddressLine1 xmlns="">50 W TOWN ST</AddressLine1>
        <AddressLine2 xmlns="">STE 400</AddressLine2>
        <City xmlns="">COLUMBUS</City>
        <State xmlns="">OH</State>
        <Zip xmlns="">43215</Zip>
        <Zip4 xmlns=""/>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope> 

下面是我的 c# 代码来创建像上面这样的 xml

        XmlDocument doc = new XmlDocument();
        XmlNode docNode = doc.CreateElement("SOAP-ENV:Envelope");
        XmlAttribute typeAttr = doc.CreateAttribute("xmlns:SOAP-ENV");
        typeAttr.Value = "http://schemas.xmlsoap.org/soap/envelope/";
        docNode.Attributes.Append(typeAttr);
        doc.AppendChild(docNode);

        XmlNode AddressRequestBody = doc.CreateElement("SOAP-ENV:Body");
        docNode.AppendChild(AddressRequestBody);

        ........
        ........

        XmlNode Zip4Node = doc.CreateElement("Zip4");
        typeAttr = doc.CreateAttribute("xmlns");
        typeAttr.Value = "";
        Zip4Node.Attributes.Append(typeAttr);
        Zip4Node.AppendChild(doc.CreateTextNode(""));
        AddressRequestBody.AppendChild(Zip4Node);

通过上面的代码,我得到了如下所示的 xml。信封和正文标记中缺少 SOAP-ENV:。关于如何获得 SOAP-ENV 的任何想法:在信封和正文标签中。我是 xml 新手,不知道如何获取。

<Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
 <Body>
  <AddressLine1 xmlns="">Add1</AddressLine1>
  <AddressLine2 xmlns="">Add2</AddressLine2>
  <City xmlns="">City1</City>
  <State xmlns="">state1</State>
  <Zip xmlns="">zip1</Zip>
  <Zip4 xmlns=""></Zip4>
 </Body>
</Envelope>

【问题讨论】:

  • 你应该使用XElement
  • 您的 XML 无效; XML 文档必须只有一个根。
  • 相关或重复:This document already has a ' DocumentElement ' node。这足以回答您的问题吗?
  • dbc,我得到了答案,但我的 xml 就像 schemas.xmlsoap.org/soap/envelope">。我怎样才能得到像 schemas.xmlsoap.org/soap/envelope"> 这样的输出。 SOAP-ENV:丢失
  • @VenkatKrishna - 您可能需要edit 您的问题并添加一个minimal reproducible example 来演示新问题。从您的评论中,我不太确定您的第二个问题是什么。

标签: c# .net xml


【解决方案1】:

您可以使用XML Document Object Model 创建所需的 XML,如下所示:

var soapNs = @"http://schemas.xmlsoap.org/soap/envelope/";
var bodyNs = @"";

var doc = new XmlDocument();
var root = doc.AppendChild(doc.CreateElement("SOAP-ENV", "Envelope", soapNs));
var body = root.AppendChild(doc.CreateElement("SOAP-ENV", "Body", soapNs));

body.AppendChild(doc.CreateElement("", "AddressLine1", bodyNs)).InnerText = "50 W TOWN ST";
body.AppendChild(doc.CreateElement("", "AddressLine2", bodyNs)).InnerText = "STE 400";
body.AppendChild(doc.CreateElement("", "City", bodyNs)).InnerText = "COLUMBUS";
body.AppendChild(doc.CreateElement("", "State", bodyNs)).InnerText = "OH";
body.AppendChild(doc.CreateElement("", "Zip", bodyNs)).InnerText = "43215";
body.AppendChild(doc.CreateElement("", "Zip4", bodyNs)).InnerText = "";

使用这种方法,会生成以下 XML:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Body>
    <AddressLine1>50 W TOWN ST</AddressLine1>
    <AddressLine2>STE 400</AddressLine2>
    <City>COLUMBUS</City>
    <State>OH</State>
    <Zip>43215</Zip>
    <Zip4></Zip4>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

注意事项:

  1. 一个格式良好的 XML 文档必须只有一个 root element。在您问题的初始版本中,您尝试将多个节点直接添加到XmlDocument doc

    doc.AppendChild(docNode);
    // Snip some code  
    doc.AppendChild(AddressRequestNode);
    

    尝试这样做会引发This document already has a 'DocumentElement' node. 异常。

  2. 您的 XML 文档在两个命名空间中有元素:http://schemas.xmlsoap.org/soap/envelope/ 和空命名空间。在这种情况下,最简单的方法是使用 XmlDocument.CreateElement(string prefix, string localName, string namespaceURI) API 并始终明确指定命名空间 URI,而不是简单地指定命名空间查找前缀并希望您之前正确初始化了相应的 xmlns: 前缀值。

    在示例 XML 中,EnvelopeBody 节点位于 http://schemas.xmlsoap.org/soap/envelope/ 命名空间中,而它们的子节点位于空命名空间中,这在我的示例代码中有所体现。

  3. XmlNode.AppendChild(XmlNode) 返回附加的孩子,因此可以流畅地使用。

  4. 在示例 XML 中,最里面的元素被明确指定为具有默认命名空间 ""

    <AddressLine1 xmlns="">50 W TOWN ST</AddressLine1>
    

    实际上xmlns="" 是不必要的,因为在 XML 中没有在更高级别指定默认命名空间。因此,以下内容在语义上是相同且充分的:

    <AddressLine1>50 W TOWN ST</AddressLine1>
    

    如果由于某种原因您需要拥有这个多余的命名空间声明,您可以这样做:

    var line1 = body.AppendChild(doc.CreateElement("", "AddressLine1", bodyNs));
    line1.Attributes.Append(doc.CreateAttribute("xmlns")).Value = bodyNs;
    line1.InnerText = "50 W TOWN ST";
    

    这会导致

    <AddressLine1 xmlns="">50 W TOWN ST</AddressLine1>
    
  5. XmlElement.InnerText 可用于设置没有子元素的元素的文本值。这比调用 CreateTextNode() 然后将结果附加到 DOM 更简洁。

工作示例 .Net fiddle here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多