Hannes Haas
Hannes Haas

Reputation: 21

Troubles with xsi:schemaLocation

I found multiple answers on the topic here and on other places in the web. But I could not figure out a solution to create the following sequence in the HTML:

<?xml version="1.0" encoding="UTF-8" ?>
<RSIL:Project xmlns:RSIL="http://www.url.com/RSIL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:eQ="http://www.url.com/eQ" xsi:schemaLocation="http://www.url.com/RSIL Name_RSIL.xsd">

Somehow either the xsi prefix of schemaLocation is missing or it is set to d1p1. Current code, depending on outcome looks similar to this one:

XmlElement projectElement = rsil.CreateElement("RSIL", "Project", @"http://www.url.com/RSIL");
rsil.AppendChild(projectElement);
projectElement.SetAttribute("xmlns:xsi", @"http://www.w3.org/2001/XMLSchema-instance");
projectElement.SetAttribute("xmlns:eQ", @"http://www.url.com/eQ");
XmlAttribute grAt = rsil.CreateAttribute("xsi", "schemaLocation", "http://www.url.com/RSIL Name_RSIL.xsd");
projectElement.Attributes.Append(grAt);

Upvotes: 2

Views: 1844

Answers (3)

humbertropolis
humbertropolis

Reputation: 9

See this article

https://social.msdn.microsoft.com/Forums/en-US/fa7205f7-f5f5-40bb-9b14-cb64c1ba5665/schemalocation-problem-with-serialization?forum=xmlandnetfx


Usually the schemaLocation attribute belongs into the namespace http://www.w3.org/2001/XMLSchema-instance and has the prefix xsi. You can achieve that as follows:

Code Snippet

public class exampleClass

{

public exampleClass() { }

[System.Xml.Serialization.XmlAttributeAttribute(AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]

public string schemaLocation = "http://www.rewerse.net/I1/2006/R2ML http://oxygen.informatik.tu-cottbus.de/R2ML/0.4/R2ML.xsd";

}



XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();

namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");

exampleClass example = new exampleClass();

XmlSerializer serializer = new XmlSerializer(typeof(exampleClass));

serializer.Serialize(Console.Out, example, namespaces);

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167401

Using LINQ to XML you can use

    XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance",
               RSIL = "http://www.url.com/RSIL",
               eQ = "http://www.url.com/eQ";

    XDocument doc = new XDocument(
        new XElement(RSIL + "Project",
                     new XAttribute(XNamespace.Xmlns + "RSIL", RSIL),
                     new XAttribute(XNamespace.Xmlns + "xsi", xsi),
                     new XAttribute(XNamespace.Xmlns + "eQ", eQ),
                     new XAttribute(xsi + "schemaLocation", "http://www.url.com/RSIL Name_RSIL.xsd")));

    Console.WriteLine(doc);

See https://dotnetfiddle.net/iP2Gse.

If you need to use the DOM implementation in the .NET framework then

    string xsi = "http://www.w3.org/2001/XMLSchema-instance",
               RSIL = "http://www.url.com/RSIL",
               eQ = "http://www.url.com/eQ";

    XmlDocument doc = new XmlDocument();

    XmlElement project = doc.CreateElement("RSIL:Project", RSIL);

    project.SetAttribute("xmlns:RSIL", RSIL);
    project.SetAttribute("xmlns:xsi", xsi);
    project.SetAttribute("xmlns:eQ", eQ);

    XmlAttribute schemaLoc = doc.CreateAttribute("xsi", "schemaLocation", xsi);
    schemaLoc.Value = "http://www.url.com/RSIL Name_RSIL.xsd";

    project.SetAttributeNode(schemaLoc);

    doc.AppendChild(project);


    Console.WriteLine(doc.OuterXml);

seems to do the job.

Upvotes: 2

Kim Homann
Kim Homann

Reputation: 3229

Using XmlTextWriter you can do it like this:

var writer = new XmlTextWriter("output.xml", Encoding.UTF8)
{
    Formatting = Formatting.Indented,
    Indentation = 1,
    IndentChar = '\t',
    Namespaces = true,
};

writer.WriteStartDocument();

writer.WriteStartElement("RSIL", "Project", "http://www.url.com/RSIL");
writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xmlns:eQ", "http://www.url.com/eQ");
writer.WriteAttributeString("xsi:schemaLocation", "http://www.url.com/RSIL Name_RSIL.xsd");
writer.WriteEndElement();

writer.WriteEndDocument();

writer.Flush();
writer.Close();

This results in at least almost exactly what you wanted:

<RSIL:Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:eQ="http://www.url.com/eQ" xsi:schemaLocation="http://www.url.com/RSIL Name_RSIL.xsd" xmlns:RSIL="http://www.url.com/RSIL" />

Upvotes: 0

Related Questions