views:

119

answers:

3

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?

+1  A: 

I think it means that true will copy the attributes from the XmlReader, because of this function in the XmlReader class GetAttribute (When overridden in a derived class, gets the value of an attribute.)

EDIT: An attribute is the date part (1st line)

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

Maybe the date doesn't get written automaticly. What about trying the the function with the boolean set to true and false. Maybe you can spot the differences and post here :)

PoweRoy
Can you elaborate your reply a bit? I have to admit I did not grok it.
mark
it means that It will not only copy the attributes explicitly specified, but also those with default values.
John Saunders
+2  A: 

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.

bill seacham
`xmlns` is not what they mean by "default attribute". See my answer.
John Saunders
+4  A: 

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"&gt;
    <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;
            }
        }
    }
}
John Saunders
+1 (10 if i could) for the well thought out and tested answer. To bad the OP accepted the other answer without any verification.
Metro Smurf