Search code examples
xmlwriter

Why XmlWriter gives me different results when I create an element with prefix and namespace depending on whether it is nested or not


I need to generate this output:

<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" />

and I do it this way:

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);
    }

But this element should be nested, and I do this way:

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);
    }

And I don't know why, the result of nested element its different, since the attribute "xmlns:xsi" is not generated.

<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" />

The example in Fiddle here.


Solution

  • The xsi prefix is defined in parent node, therefore the writer omits inherited prefix definition.