简体   繁体   English

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

[英]“xsi:type” attribute is changed to “type” c# xml document

I'm using XMLDocument to create a XML file, but when i set an attribute to an element called "xsi:type" in the.xml generated file this attribute is changed to just "type".我正在使用 XMLDocument 创建一个 XML 文件,但是当我在 .xml 生成的文件中为名为“xsi:type”的元素设置属性时,此属性更改为“type”。

This is the output that i'm expecting:这是我期待的 output:

<ODX xsi:type="VALUE" />

This is my code这是我的代码

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

This is content of the xml_question_test.xml output file that i get,:这是我得到的 xml_question_test.xml output 文件的内容:

<ODX type="VALUE" />

Notice how change the name of the attribute from "xsi:type" to "type", i tried to set the name of the attribute as literal with @ before the string but it didn't work... i haven't found anything useful...请注意如何将属性的名称从“xsi:type”更改为“type”,我尝试将属性的名称设置为在字符串之前使用 @ 的文字,但它不起作用......我没有找到任何东西有用...

Since you are adding xs , you need to specify the namespace which it represents.由于您要添加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");
    }

You can read more here about XML namespaces: https://www.w3schools.com/xml/xml_namespaces.asp您可以在此处阅读有关 XML 命名空间的更多信息: https://www.w3schools.com/xml/xml_namespaces.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM