6

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Scott Kramer
  • 1,711
  • 3
  • 24
  • 37
  • 1
    You haven't mentioned which programming language you're using. One assumes C# because WriteStartDocument is a method on XmlWriter, but that's not a guarantee. – Randolpho May 29 '09 at 21:01
  • It says "C#" in the middle of the line just below the XML fragment..... – marc_s May 29 '09 at 21:11

2 Answers2

8

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();
baretta
  • 7,385
  • 1
  • 25
  • 25
5

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();
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900