小编Wol*_*ues的帖子

如何将 xsi:noNamespaceSchemaLocation 添加到序列化器

我构建了一个 XML 文档,需要根据 xsd 文件进行验证。因此,我需要在 xml 的根元素中引用 xsd 文件。到目前为止我使用这个 C# 代码:

var ser = new XmlSerializer(typeof(myspecialtype));
XmlSerializerNamespaces MainNamespace = new XmlSerializerNamespaces();
MainNamespace.Add("xlink", "http://www.w3.org/1999/xlink");
MainNamespace.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
using (XmlWriter w = XmlWriter.Create(@"C:\myxmlfile.xml"))
{
    w.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"utils/somexsl.xsl\"");
    ser.Serialize(w, LeBigObject, HauptNs);
}
Run Code Online (Sandbox Code Playgroud)

生成的 Xml 开头如下:

var ser = new XmlSerializer(typeof(myspecialtype));
XmlSerializerNamespaces MainNamespace = new XmlSerializerNamespaces();
MainNamespace.Add("xlink", "http://www.w3.org/1999/xlink");
MainNamespace.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
using (XmlWriter w = XmlWriter.Create(@"C:\myxmlfile.xml"))
{
    w.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"utils/somexsl.xsl\"");
    ser.Serialize(w, LeBigObject, HauptNs);
}
Run Code Online (Sandbox Code Playgroud)

但我需要这个:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="utils/somexsl.xsl"?>
<caddy-xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlVersion="03.07.00">
Run Code Online (Sandbox Code Playgroud)

我在这里遇到了“CreateAttribute”: …

c# xml xsd

0
推荐指数
1
解决办法
5719
查看次数

标签 统计

c# ×1

xml ×1

xsd ×1