Can someone explain to me what the value of the logical parameter of the System.Xml.XmlDictionaryWriter.WriteNode (XmlReader, bool) method means?

According to MSDN:
defattr
Type: System.Boolean
If true , copy the default attributes from XmlReader ; otherwise false . If true , use the default attributes; otherwise false .

And my question is what does this author mean?

+3
source share
3 answers

xml . xml, , XML- , MoveToNextAttribute(), xmlns, w3c uri ( uri -- xmlns = 'http://www.w3c.org/schema' ReadOuterXml() .

+3

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>

XmlReader, :

<?xml version="1.0" encoding="utf-8" ?>
<HasDefaultAttributes xmlns="http://tempuri.org/ElementWithDefaultAttributes.xsd" 
   nodefault="none">
  <Inner>text</Inner>
</HasDefaultAttributes>

, 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>

:

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;
            }
        }
    }
}
+4

, , true XmlReader - XmlReader GetAttribute ( , .)

EDIT: (1- )

<note date="12/11/2002">  
<to>Tove</to>   <from>Jani</from>  
<heading>Reminder</heading>  
<body>Don't forget me this weekend!</body> </note>

, . , true false. , :)

+1

Source: https://habr.com/ru/post/1725023/


All Articles