LINQ - Linq to XML XAttribute name/value pair not allowing colon (:)

Asked By Janet McGrath on 14-Jan-10 05:01 PM

I’m using Linq to XML to format a very complex xml tree and am trying to format these lines below:

 

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

 

<MIF xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mifv1.xsd" ISO-CODE="100028">

 

Below is my code:

Dim xmlTree As New XDocument(New XDeclaration("1.0", "UTF-8", "X"), _

  New XElement("MIF", _

  New XAttribute("xmlnsxsi", "http://www.w3.org/2001/XMLSchema-instance"), _

  New XAttribute("xsinoNamespaceSchemaLocation", "mifv1.xsd"), _

  New XAttribute("ISO-CODE", "100028"), _

  New XElement(Me.NewContractNode())))

 

The XDocument / XDeclaration is working on formatting the 1st line.  The 2nd line is more problematic, as the XAttribute name argument (xmlns:xsi) won’t accept a colon (:) character; as with the xsi:noNamespaceSchemaLocation. 

 

I have a work around, where I convert xmlTree to a string and replace values with ones containing colons, but there are 2 problems with this:

1) it looses all the information from the XDeclaration, so I’m manually prepending it to my string

2) I now have to use StreamWriter instead of xmlTree.save() to write the result:

 

Dim sb As New StringBuilder

sb.Append("<?xml version=")

sb.Append(Chr(34))

sb.Append("1.0")

sb.Append(Chr(34))

sb.Append(" encoding=")

sb.Append(Chr(34))

sb.Append("UTF-8")

sb.Append(Chr(34))

sb.Append(" ?>")

sb.Append(xmlTree.ToString())

 

workXML = sb.ToString()

workXML = Replace(workXML, "xmlnsxsi", "xmlns:xsi")

workXML = Replace(workXML, "xsinoNamespaceSchemaLocation", "xsi:noNamespaceSchemaLocation")

 

outFile.WriteLine(workXML)

outFile.Close()

 

My workaround is working, but it seems to me that there must be a better way of doing this.  I’d appreciate any suggestions.

 

 

XmlWriter class, XmlWriterSettings && OmitXmlDeclaration - Hamit YILDIRIM replied to Janet McGrath on 14-Jan-10 06:29 PM

Hi,

I think you can use XmlWriter Class like below

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            const string ns1 = "http://firstline.com/";
            const string xsi = "http://www.w3.org/2001/XMLSchema-instance";

            using (XmlWriter writer = XmlWriter.Create(Console.Out, settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("myRoot", ns1);
                writer.WriteAttributeString("SchemaVersion", "1.0");
                writer.WriteAttributeString("xmlns", "xsi", "http://www.w3.org/2000/xmlns/", xsi);
                writer.WriteAttributeString("xsi", "schemaLocation", xsi, ns1 + " schema1.xs");

                writer.WriteStartElement("element1", ns1);
                writer.WriteElementString("test1", ns1, "test value");
                writer.WriteElementString("test2", ns1, "value 2");

                writer.WriteEndDocument();
            }


Also XmlWriterSettings setting could ve set like below for the namespace


XmlWriterSettings wset = new XmlWriterSettings(); 
wset.OmitXmlDeclaration = true; // I think you will make false

http://www.csharper.net/blog/serializing_without_the_namespace__xmlns__xmlns_xsd__xmlns_xsi_.aspx

Yes, the non-alpha Colon character is reserved for XML. - [)ia6l0 iii replied to Janet McGrath on 14-Jan-10 10:01 PM

You would use the XMLConvert class to escape the non-alpha character - colon (:)  that is reserved for namespaces and escape it with underscore.

The method follows a simple syntax like:
XmlConvert.EncodeName(sting yourXML) 

More info can be obtained from MSDN here.

Also, you could use the SecurityElement's Escape method that replaces invalid XML characters in a string with their valid XML equivalent. Again see MSDN article here for examples.


XMLConvert - Hamit YILDIRIM replied to [)ia6l0 iii on 15-Jan-10 11:12 AM

Hi [)ia6l0 iii

I did not see that is Greate,