locked
xsi:noNamespaceSchemaLocation

    Question

  •  

    Hi,

     

    I would like to produce a document with the following element using LINQ.

     

    This code

     

    new XAttribute(XNamespace.Xmlns + "xsi", http://www.w3.org/2001/XMLSchema-instance)

     

    creates

     

    xsi:noNamespaceSchemaLocation="contracts.xsd"

     

    but I also need to create xsi:noNamespaceSchemaLocation="contracts.xsd" . It seems no XNamspace exist for xsi.

     

    Has anyone go an idea?

     

    Thanks

     

    Glenn

     

    Thursday, May 29, 2008 6:42 PM

Answers

  • For anyone looking for the answer.

     

    xsi is only a prefix to make XML documents more readable. The classes in the
    System.Xml namespace will automatically generate the prefix if there is
    prefix defined the scope of the current node, but you have to provide the
    full namespace, not the prefix:

     

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

    new XAttribute(XNamespace.Xmlns + "xsi", http://www.w3.org/2001/XMLSchema-instance);

    new XAttribute(ns + "noNamespaceSchemaLocation", "contracts.xsd");

     

    Friday, May 30, 2008 11:51 AM

All replies

  • Don't know if I understand you, but seems that you want something like this

    Code Snippet

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



    To do this all you need is something this

    Code Snippet

    XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
                XDocument doc = new XDocument(
                    new XElement("Root",
                        new XAttribute(XNamespace.Xmlns + "xsi", xsi)));

               
                doc.Save(Console.Out);



    Pedro Rainho



    Friday, May 30, 2008 9:44 AM
  • Thanks for your reply but I need to produce xsi:noNamespaceSchemaLocation="contracts.xsd" not xmlns:xsi

    Friday, May 30, 2008 11:35 AM
  • For anyone looking for the answer.

     

    xsi is only a prefix to make XML documents more readable. The classes in the
    System.Xml namespace will automatically generate the prefix if there is
    prefix defined the scope of the current node, but you have to provide the
    full namespace, not the prefix:

     

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

    new XAttribute(XNamespace.Xmlns + "xsi", http://www.w3.org/2001/XMLSchema-instance);

    new XAttribute(ns + "noNamespaceSchemaLocation", "contracts.xsd");

     

    Friday, May 30, 2008 11:51 AM
  • Here is a sample that create the namespace declaration and the noNamespaceSchemaLocation attribute:

    Code Snippet

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

    XElement foo = new XElement("foo",

    new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),

    new XAttribute(xsi + "noNamespaceSchemaLocation", "contracts.xsd"));

    foo.Save(Console.Out);

     

     

    Result is

    Code Snippet

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

     

     

    Friday, May 30, 2008 12:00 PM
  • This approach also works for classes generated by XSD.EXE.  I added an attribute to the Root element because I wanted the .xsd to be referenced.  The syntax is slightly different, but the effect is the same.

     

    '''Manually added to reference schema source

    <remarks/>

    <System.Xml.Serialization.

    XmlAttributeAttribute([Namespace]:="http://www.w3.org/2001/XMLSchema-instance", [AttributeName]:="noNamespaceSchemaLocation", [DataType]:="string"

    )> _

     

    Public defaultSchema As String =

    "POInProcessBatch_v5.xsd"

     

     

    produces the following Attribute within the Root element when Serialized:

    <?xml version="1.0" encoding="utf-8"?>

     

     

    <POInProcessBatch xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" batchNumber="9F7AK1D64481" xsi:noNamespaceSchemaLocation="POInProcessBatch_v5.xsd">

     

     

    mp
    Monday, October 10, 2011 11:48 PM