“ xsi:type”属性更改为“ type” c#xml文档

问题描述 投票:0回答:1

我正在使用XMLDocument创建XML文件,但是当我在.xml生成的文件中将属性设置为名为“ xsi:type”的元素时,该属性将更改为“ type”。

这是我期望的输出:

<ODX xsi:type="VALUE" />

这是我的代码

using System.Xml;
        public static void xml_test()
        {
            XmlDocument doc = new XmlDocument();
            XmlDeclaration declaire = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            XmlElement ODX = doc.CreateElement("ODX");
            ODX.SetAttribute("xsi:type", "VALUE");
            doc.AppendChild(ODX);
            doc.Save("C:\\Users\\dev\\Pictures\\DocParser\\DocParser\\xml_question_test.xml");
        }

这是我得到的xml_question_test.xml输出文件的内容,:

<ODX type="VALUE" />

[请注意如何将属性名称从“ xsi:type”更改为“ type”,我尝试在字符串之前使用@将属性名称设置为文字,但是它没有用...我没有发现有用的东西...

c# xml xml-serialization
1个回答
1
投票

由于要添加xs,因此需要指定它代表的名称空间。

public static void xml_test()
    {
        XmlDocument doc = new XmlDocument();
        XmlDeclaration declaire = doc.CreateXmlDeclaration("1.0", "utf-8", null);
        XmlElement ODX = doc.CreateElement("ODX");
        var attr = doc.CreateAttribute("xs:type", "http://www.w3.org/2001/XMLSchema");
        attr.Value = "VALUE";
        ODX.Attributes.Append(attr);
        doc.AppendChild(ODX);
        doc.Save("C:\\Users\\nemmil\\OneDrive - Snow Software\\Documents\\Creative Cloud user guide\\xml_question_test.xml");
    }

您可以在此处阅读有关XML名称空间的更多信息:https://www.w3schools.com/xml/xml_namespaces.asp

© www.soinside.com 2019 - 2024. All rights reserved.