Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anyone explain to me what is the meaning of the boolean parameter of the System.Xml.XmlDictionaryWriter.WriteNode(XmlReader, bool) method?

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

And my question is what does the author mean by this?

like image 306
mark Avatar asked Nov 01 '25 08:11

mark


2 Answers

An XML Schema can define certain attributes as having default values. I think this is referring to those attributes - should they be returned, with their default values, when they are not explicitly specified?


I have confirmed this. I created the following schema:

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

I read the following document through an XmlReader configured with the schema:

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

Despite this, when I used XmlDictionaryWriter.WriteNode(reader, true), I got the following result:

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

Code:

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;
            }
        }
    }
}
like image 101
John Saunders Avatar answered Nov 03 '25 21:11

John Saunders


Every xml element has a default attribute. Even if there is no attribute when you examine the raw xml, you will find that if you step thru any xml parser code when it uses a method like MoveToNextAttribute() there will be an xmlns attribute whose value is a w3c uri(I dont recall the exact uri -something like xmlns='http://www.w3c.org/schema' You will also see it in the output from the ReadOuterXml() method for any element.

like image 23
bill seacham Avatar answered Nov 03 '25 22:11

bill seacham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!