SharpNoiZy
SharpNoiZy

Reputation: 1119

How do I write this special attributes with XmlWriter?

I tried multiple times to write the following XML node with the XmlWriter class, but I don't get it ;(

<document xmlns="abc:def-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="abc:def-org:v3 test.xsd">

Can someone help me?
Kind regards

Upvotes: 0

Views: 49

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064204

using(var writer = XmlWriter.Create(...))
{
    writer.WriteStartElement("document", "abc:def-org:v3");
    writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
    writer.WriteAttributeString("schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "abc:def-org:v3 test.xsd");
    // ...
    writer.WriteEndElement();
}

Note in particular that when we add schemaLocation, we don't add xsi:schemaLocation, but rather we add "schemaLocation in the http://www.w3.org/2001/XMLSchema-instance namespace", and XmlWriter maps this to xsi. If you always talk in terms of namespaces rather than prefixes, you won't have any nasty surprises when you change prefix.

Upvotes: 2

Related Questions