简体   繁体   中英

How to get xml namespaces declared globally

I am having a problem of getting multiple namespace declarations in elements, not in a central global place. Reason is I am creating XElements with namespaces, then adding those to the root element. Is there a simple method to avoid this? Here is a simplified code example:

    private void testcode()
    {
        XNamespace ns_xsi = "http://www.w3.org/2001/XMLSchema-instance";
        XNamespace ns_xsd = "http://www.w3.org/2001/XMLSchema";
        XNamespace ns = "InstrumentMeasurement";

        // top-level element
        XElement XResults = new XElement(ns + "Results");

        // this will happen many times
        for(int i = 0; i<3; i++)
        {
            XElement XResult =
                new XElement(ns + "Result",
                    new XAttribute(ns_xsi + "nil", true));
            XResults.Add(XResult);
        }

        // complete the XDoc and write to file
        XDocument XReport = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
        XReport.Add(XResults);
        string strContent = XReport.Declaration.ToString() + Environment.NewLine + XReport;
        System.IO.File.WriteAllText(@"c:\temp\doc.xml", strContent);
        Console.WriteLine(@"written testfile c:\temp\doc.xml");
        return;
    }

and here is the resulting xml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Results xmlns="InstrumentMeasurement">
  <Result p2:nil="true" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" />
  <Result p2:nil="true" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" />
  <Result p2:nil="true" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" />
</Results>

instead of the much cleaner looking desired version:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Results xmlns="InstrumentMeasurement
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Result xsi:nil="true" />
  <Result xsi:nil="true" />
  <Result xsi:nil="true" />
</Results>

(by the way, the attribute is not rubbish, as I first thought, see eg http://www.w3.org/TR/xmlschema-1/#xsi_nil )

Ok I found the answer myself (RTFM) in C# 3.0 in a nutshell (J+B Albahari, O'Reilly, 3rd ed, 2007, p.386, free on google books and other sites).

"To hint the serializer prior to writing the XML:"

XResults.SetAttributeValue(XNamespace.Xmlns + "xsi", ns_xsi);
XResults.SetAttributeValue(XNamespace.Xmlns + "xsd", ns_xsd);

Not only does this solve my original problem, but also how to add a namespace declaration that is not referred to, but still required by my project.

The nice, desired, output is now:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Results xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        xmlns="InstrumentMeasurement">
  <Result xsi:nil="true" />
  <Result xsi:nil="true" />
  <Result xsi:nil="true" />
</Results>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM