谁能向我解释 System.Xml.XmlDictionaryWriter.WriteNode(XmlReader, bool) 方法的布尔参数的含义是什么?

mar*_*ark 4 .net xml xmlreader

根据 MSDN:
defattr
类型:System.Boolean
如果true ,则从XmlReader复制默认属性;否则为 false。如果为true,则使用默认属性;否则为

我的问题是作者这样说是什么意思?

Joh*_*ers 5

XML 模式可以将某些属性定义为具有默认值。我认为这是指那些属性 - 当未明确指定它们时,是否应该返回它们及其默认值?


我已经证实了这一点。我创建了以下架构:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="ElementWithDefaultAttributes"
    targetNamespace="http://tempuri.org/ElementWithDefaultAttributes.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/ElementWithDefaultAttributes.xsd"
    xmlns:mstns="http://tempuri.org/ElementWithDefaultAttributes.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:complexType name="HasDefaultAttributesType">
    <xs:sequence>
      <xs:element name="Inner"/>
    </xs:sequence>
    <xs:attribute name="default1" default="value1" type="xs:string"/>
    <xs:attribute name="nodefault" type="xs:string"/>
    <xs:attribute name="default2" default="value2" type="xs:string"/>
  </xs:complexType>
  <xs:element name="HasDefaultAttributes" type="mstns:HasDefaultAttributesType"/>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)

XmlReader我通过配置架构阅读了以下文档:

<?xml version="1.0" encoding="utf-8" ?>
<HasDefaultAttributes xmlns="http://tempuri.org/ElementWithDefaultAttributes.xsd" 
   nodefault="none">
  <Inner>text</Inner>
</HasDefaultAttributes>
Run Code Online (Sandbox Code Playgroud)

尽管如此,当我使用时XmlDictionaryWriter.WriteNode(reader, true),我得到了以下结果:

<?xml version="1.0" encoding="utf-16"?>
<HasDefaultAttributes nodefault="none" default1="value1" default2="value2" 
   xmlns="http://tempuri.org/ElementWithDefaultAttributes.xsd">
    <Inner>text</Inner>
</HasDefaultAttributes>
Run Code Online (Sandbox Code Playgroud)

代码:

public static XDocument DefaultAttributes()
{
    var nt = new NameTable();
    var schemas = new XmlSchemaSet(nt);
    using (
        var schemaText =
            File.OpenText(
                @"..\..\XmlDictionaryWriter\ElementWithDefaultAttributes.xsd"))
    {
        var schema = XmlSchema.Read(schemaText, ValidationEventHandler);
        schemas.Add(schema);
    }

    var settings = new XmlReaderSettings
                   {
                       ValidationType = ValidationType.Schema,
                       Schemas = schemas
                   };
    settings.ValidationEventHandler += ValidationEventHandler;

    using (
        var dataText =
            File.OpenText(
                @"..\..\XmlDictionaryWriter\HasDefaultAttributes.xml"))
    {
        using (var outputStream = new MemoryStream())
        {
            using (
                var xdw =
                    System.Xml.XmlDictionaryWriter.CreateTextWriter(
                        outputStream, Encoding.UTF8, false))
            {
                using (var reader = XmlReader.Create(dataText, settings))
                {
                    while (reader.Read())
                    {
                        xdw.WriteNode(reader, true);
                    }
                }
            }

            outputStream.Position = 0;
            using (var output = new StreamReader(outputStream))
            {
                var doc = XDocument.Load(output);
                return doc;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)