首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在创建带有前缀和命名空间的元素时,根据元素是否嵌套,XmlWriter会给出不同的结果

为什么在创建带有前缀和命名空间的元素时,根据元素是否嵌套,XmlWriter会给出不同的结果
EN

Stack Overflow用户
提问于 2018-03-03 01:54:43
回答 1查看 19关注 0票数 0

我需要生成以下输出:

代码语言:javascript
复制
<complement12:Complement xsi:schemaLocation="http://www.example.com/complement12 http://www.example.com/sub/files/complement12.xsd" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:complement12="http://www.example.com/complement12" />

我是这样做的:

代码语言:javascript
复制
var settings = new XmlWriterSettings {Indent = true};

    using (var sw = new StringWriter())
    {
        using (var writer = XmlWriter.Create(sw, settings))
        {
            writer.WriteStartDocument();

                writer.WriteStartElement("complement12", "Complement", @"http://www.example.com/complement12");                

                writer.WriteAttributeString("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance",
                    "http://www.example.com/complement12 http://www.example.com/sub/files/complement12.xsd");

                writer.WriteAttributeString("version", "1.1");

                writer.WriteEndElement();

            writer.WriteEndDocument();
        }

        Console.WriteLine(sw);
    }

但是这个元素应该是嵌套的,我是这样做的:

代码语言:javascript
复制
var settings = new XmlWriterSettings {Indent = true};

    using (var sw = new StringWriter())
    {
        using (var writer = XmlWriter.Create(sw, settings))
        {
            writer.WriteStartDocument();

            writer.WriteStartElement("doc", "Document", @"http://www.example.com/sub");

                writer.WriteAttributeString("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance",
                    "http://www.example.com/sub http://www.example.com/sub/files/doc20.xsd");

                writer.WriteStartElement("complement12", "Complement", @"http://www.example.com/complement12");                

                writer.WriteAttributeString("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance",
                    "http://www.example.com/complement12 http://www.example.com/sub/files/complement12.xsd");

                writer.WriteAttributeString("version", "1.1");

                writer.WriteEndElement();

            writer.WriteEndElement();

            writer.WriteEndDocument();
        }

        Console.WriteLine(sw);
    }

我不知道为什么,嵌套元素的结果是不同的,因为没有生成属性"xmlns:xsi“。

代码语言:javascript
复制
<doc:Document xsi:schemaLocation="http://www.example.com/sub http://www.example.com/sub/files/doc20.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:doc="http://www.example.com/sub"><complement12:Complement xsi:schemaLocation="http://www.example.com/complement12 http://www.example.com/sub/files/complement12.xsd" version="1.1" xmlns:complement12="http://www.example.com/complement12" />

小提琴here中的示例。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-03 02:09:41

xsi前缀是在父节点中定义的,因此编写器省略了继承的前缀定义。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49074738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档