如何使用XmlSerializer在xml头中将自定义值设置为xmlns

本文关键字:自定义 设置 xmlns XmlSerializer 何使用 xml | 更新日期: 2023-09-27 18:19:55

我正在尝试使用XmlSerializer将Document标记中的xmlns属性值设置为自定义值。目前,我的简化xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd">
  <GrpHdr>
    <Price Curency="EUR">
      40.55
    </Price>
  </GrpHdr>
</Document>

我的简化代码如下:

public void Main(){
    var document = new CustomDocument();
    new XmlSerializer(typeof(CustomDocument)).Serialize(Console.Out, document);
}
[XmlRoot(ElementName = "Document")]
public class CustomDocument{
    [XmlAttribute("schemaLocation", AttributeName = "schemaLocation",
        Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string SchemaLocation = "urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd";
    [XmlElement("GrpHdr")
    Public XmlHeader {get; set;}
    Public XmlDocument(){
        XmlHeader = new XmlHeader();
    }
}
public class XmlHeader{
    [XmlElement("Price")
    Public string Price {get; set;}
    public XmlHeader(){
        Price = "40.55";
    }
}

如何更改xmlns:xsd的值?添加[XmlElement("xmlns")]并不能达到的效果

如何使用XmlSerializer在xml头中将自定义值设置为xmlns

很遗憾,您不被允许这样做。xmlns:xsixmlns:xsd是保留的名称空间。您不能更改默认值,因为它是.Net框架中提供的标准模式定义的一部分。

我不确定您为什么要这样做,但如果您想添加与架构xsd的命名空间匹配的命名空间,那么您可以添加一个自定义命名空间,如:

 [XmlRoot(ElementName = "Document", Namespace = "http://customNameSpaceFromXsd/XMLSchema.xsd")]
    public class CustomDocument{
    }

这将出现在您的xml中,如:

<Document 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://customNameSpaceFromXsd/XMLSchema.xsd"
xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd">

使用XML Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //<?xml version="1.0" encoding="utf-8"?>
            //<Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd">
            //  <GrpHdr>
            //    <Price Curency="EUR">
            //      40.55
            //    </Price>
            //  </GrpHdr>
            //</Document>
            string xml =
               "<?xml version='"1.0'" encoding='"utf-8'"?>" +
               "<Document xmlns:xsd='"http://www.w3.org/2001/XMLSchema'" xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance'" xsi:schemaLocation='"urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd'">" +
               "</Document>";
            XDocument doc = XDocument.Parse(xml);
            XElement document = (XElement)doc.FirstNode;
            XNamespace xsd = document.GetNamespaceOfPrefix("xsd");
            XNamespace xsi = document.GetNamespaceOfPrefix("xsi");
            document.Add(new XElement("GrpHddr",
                    new XElement("Price", new object[] {
                        new XAttribute("Currency", "EUR"),
                        40.55
                    })
            ));
        }
    }
}
​