Nick LaMarca
Nick LaMarca

Reputation: 8188

Adding an xsd file value to an XmlAttribute

How I am creating an xml string of proper xml with the code below..

string myInputXmlString = @"<ApplicationData>
                                        <something>else</something>
                                    </ApplicationData>";
        var doc = new XmlDocument();
        doc.LoadXml(myInputXmlString);

        XmlAttribute newAttr = doc.CreateAttribute(
            "xsi", 
            "noNamespaceSchemaLocation", 
            "http://www.w3.org/2001/XMLSchema-instance");
        doc.DocumentElement.Attributes.Append(newAttr);

        var ms = new MemoryStream();
        XmlWriterSettings ws = new XmlWriterSettings
        {
            OmitXmlDeclaration = false,
            ConformanceLevel = ConformanceLevel.Document,
            Encoding = UTF8Encoding.UTF8
        };
        var tx = XmlWriter.Create(ms, ws);
        doc.Save(tx);
        tx.Flush();

        var xmlString = UTF8Encoding.UTF8.GetString(ms.ToArray());
        Console.WriteLine(xmlString);

How do I add the xsd information to this so the xml looks like this (with "FullModeDataset.xsd" includded?

 <ApplicationData
  xsi:noNamespaceSchemaLocation="FullModeDataset.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />

Instead of this which the current code is outputing

 <ApplicationData
  xsi:noNamespaceSchemaLocation=""
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />

Upvotes: 0

Views: 332

Answers (1)

MisterIsaak
MisterIsaak

Reputation: 3922

Does this work by chance?

doc.DocumentElement.SetAttribute("noNamespaceSchemaLocation", 
        "http://www.w3.org/2001/XMLSchema-instance",
        "FullModeDataset.xsd");

Upvotes: 1

Related Questions