Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add xml namespaces

This feed (snippit of it) needs to look exactly like this:

<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">

what do I add to this C# code to add that extra xmlns, xsi junk:

writer.WriteStartDocument();
writer.WriteStartElement("AmazonEnvelope");

this feed is rejected without it--

like image 972
Scott Kramer Avatar asked May 29 '09 20:05

Scott Kramer


2 Answers

Try this:

writer.WriteStartElement("AmazonEnvelope");
writer.WriteAttributeString(
  "xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString(
  "xsi", "noNamespaceSchemaLocation", null, "amzn-envelope.xsd");
...
writer.WriteEndElement();
like image 132
baretta Avatar answered Sep 28 '22 03:09

baretta


Is .NET 3.5 an option?

XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";

string s = new XElement("AmazonEnvelope",
    new XAttribute(XNamespace.Xmlns + "xsi", ns),
    new XAttribute(ns + "noNamespaceSchemaLocation", "amzn-envelope.xsd")
).ToString();

or with XmlWriter:

const string ns = "http://www.w3.org/2001/XMLSchema-instance";
writer.WriteStartDocument();
writer.WriteStartElement("AmazonEnvelope");
writer.WriteAttributeString("xmlns", "xsi", "", ns);
writer.WriteAttributeString("xsi", "noNamespaceSchemaLocation",
      ns, "mzn-envelope.xsd");
writer.WriteEndDocument();
like image 25
Marc Gravell Avatar answered Sep 28 '22 03:09

Marc Gravell