"xsi:type" 属性更改为 "type" c# xml 文档
"xsi:type" attribute is changed to "type" c# xml document
我正在使用 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",我尝试将属性名称设置为在字符串前加上 @ 的文字,但它没有用。 .我还没有发现任何有用的东西...
由于您要添加 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:\xml_question_test.xml");
}
您可以在此处阅读有关 XML 命名空间的更多信息:https://www.w3schools.com/xml/xml_namespaces.asp
我正在使用 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",我尝试将属性名称设置为在字符串前加上 @ 的文字,但它没有用。 .我还没有发现任何有用的东西...
由于您要添加 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:\xml_question_test.xml");
}
您可以在此处阅读有关 XML 命名空间的更多信息:https://www.w3schools.com/xml/xml_namespaces.asp