【问题标题】:How to add a namespace and another namespace with a prefix in XML inside the doc如何在文档内的 XML 中添加一个名称空间和另一个带有前缀的名称空间
【发布时间】:2019-12-02 08:51:47
【问题描述】:

我的目标是创建一个XML node,它有两个attributes - 一个是namespace,另一个是prefix,其中namespace,如下所示:

到目前为止,我已经尝试了 3 个选项:

选项 1:硬编码,不起作用。

        XElement paymentmethodType = XElement.Parse("<PaymentMethod xmlns:xsi=\"w3.org/2001/XMLSchema-instance\" xsi:type=\"CreditTransferType\"/>");

我只知道这个:

 <PaymentMethod xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" type="CreditTransferType">

选项 2:将我的结构与 XmlAttributes 和 XMlNodes 一起使用:

    XmlAttribute paymentmethodNamespace = CreateAttribute(paymentMethod, "xmlns:xsi", "http://w3.org/2001/XMLSchema-instance");
    XmlAttribute paymentmethodType = CreateAttribute(paymentMethod, "xsi:type", "CreditTransferType");

    public XmlAttribute CreateAttribute(XmlNode parentNode, string attributeName, string attributeValue)
    {
        var attribute = parentNode.OwnerDocument.CreateAttribute(attributeName);
        attribute.Value = attributeValue;
        parentNode.Attributes.Append(attribute);
        // parentNode.AppendChild(node);
        return attribute;
    }

我又明白了:

 <PaymentMethod xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" type="CreditTransferType">

选项 3:XElement 和 XAttributes:

        XElement paymentMethod = new XElement("PaymentMethod",
        new XAttribute(XNamespace.Xmlns + "xsi", "http://w3.org/2001/XMLSchema-instance"),
        new XAttribute(XNamespace.Xmlns + "xsi:type", "CreditTransferType"));

但我得到一个Exception,“:”不是一个有效的符号。这也是一个问题,因为我使用XMlDocument,它只适用于XMlNodeXMlAttribute。它不适用于XAttribute

现在我有超过 400 行代码,我只想设置这个命名空间,我已经完成了文档。有没有简单的方法?

【问题讨论】:

  • 您的选项 1 有效。有什么问题?
  • 嗨,jdweng。它没有得到 xsi:type 它只是得到类型,因此 Web 服务拒绝该文档。
  • 我在代码后面放了一个断点,然后将鼠标悬停在 paymentmethodType 上并选择箭头并查看 XML,它与输入相匹配。不确定您的观看方式。
  • 是的,代码显示它很好。但是,当您将其保存到 XML 时,它不会保存在 xml 本身中。

标签: c# xml namespaces attributes


【解决方案1】:

我不知道Option 1Option 2,但对于Option 3 - 你要做的是先获取namespace,然后使用namespace 分配type。因为该命名空间是 xmlns:xsi,所以您为其分配类型,它将自动匹配到 xsi:type。简而言之,这里是代码:

            XNamespace ns = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");


              new XElement("PaymentMethod",


                  new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),

                  new XAttribute(ns + "type", "CreditTransferType"));

【讨论】:

  • 我对此进行了测试,它完成了这项工作。问题是如何将它添加到我的 XML 中的节点?
【解决方案2】:

以下作品:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
         static void Main(string[] args)
         {
             string xmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><root></root>";
             XDocument doc = XDocument.Parse(xmlHeader);
             XElement root = doc.Root;

             XElement paymentmethodType = XElement.Parse("<PaymentMethod xmlns:xsi=\"w3.org/2001/XMLSchema-instance\" xsi:type=\"CreditTransferType\"/>");
             root.Add(paymentmethodType);

             doc.Save(FILENAME);
         }
    }
}

【讨论】:

    【解决方案3】:

    所以我按照 dim 的建议使用了选项 3。

    为了不必重写全部内容,我所做的是创建第二个加载 XML 的文档。

    然后第一个文档从第二个文档导入节点并将其附加到根元素,它工作正常。

                XmlDocument doc2 = new XmlDocument();
    
    
                XNamespace ns = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
    
    
                 XElement paymentMethodEl =  new XElement("PaymentMethod",
    
    
                      new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
    
                      new XAttribute(ns + "type", "CreditTransferType"));
    
                doc2.LoadXml(paymentMethodEl.ToString());
    
                XmlWriter writer2 = XmlWriter.Create(Path.Combine(Settings.Default.InvoiceXmlFolder, "doc2.xml"), settings);
                doc2.Save(writer2);
                XmlNode paymentMethod = doc.ImportNode(doc2.FirstChild, true);
                rootNode.AppendChild(paymentMethod);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-02
      • 2012-05-22
      • 2022-12-10
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多