如何使用C#将xsi:type作为XML属性添加到已经具有属性的标签中

我阅读了两个Stackoverflow答案,这些答案建议将xsi:type硬编码为Element。但是,我不能将其应用于我的情况,因为它应该在第一个attribute之后。

这是我的代码:

    XmlNode paymentMethod = CreateEmptyNode(rootNode,"PaymentMethod");

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

    XmlAttribute paymentmethodType = CreateAttribute(paymentMethod,"xsi:type","CreditTransferType");

    public XmlNode CreateEmptyNode(XmlNode parentNode,string nodename)
    {
        var node = parentNode.OwnerDocument.CreateElement(nodename);
        parentNode.AppendChild(node);
        return node;
    }

    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;
    }

这就是我得到的结果。它从类型中省略了xsi:

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

结果必须为:

   <PaymentMethod xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CreditTransferType">
shengyi0000 回答:如何使用C#将xsi:type作为XML属性添加到已经具有属性的标签中

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3150692.html

大家都在问