[Mono-list] Questions about writing an XML document

Atsushi Eno atsushi at ximian.com
Tue May 9 07:50:56 EDT 2006


Hello,

Hmm, it is a bug in XmlAttribute (it appears when xmlDoc.Save(blah) is
added). I made a simple fix which will soon go into svn.

As a workaround, you can use XmlElement.SetAttribute("xsi",
"schemaLocation", XmlSchema.InstanceNamespace).

Atsushi Eno

Loren Bandiera wrote:
> Hi,
> 
> I want to write an XML document but there are some parts I'm not sure
> how to go about doing. Specifically namespaces and schemas. Take the
> following example XML:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <doc xmlns="http://whatever.org/XMLSchema/foo"
>      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>      xsi:schemaLocation="http://whatever.org/XMLSchema/foo.xsd">
>      <foo>bar</foo>
> </doc>
> 
> To create it, I've tried doing something like this:
> 
> XmlDocument xmlDoc = new XmlDocument ();
> xmlDoc.AppendChild (xmlDoc.CreateNode
> (XmlNodeType.XmlDeclaration,"",""));
> 
> XmlElement docElement = xmlDoc.CreateElement ("doc");
> docElement.SetAttribute ("xmlns", "http://whatever.org/XMLSchema/foo");
> docElement.SetAttribute ("xmlns:xsi",
> "http://www.w3.org/2001/XMLSchema-instance");
> docElement.SetAttribute ("xsi:schemaLocation",
> "http://whatever.org/XMLSchema/foo.xsd");
> xmlDoc.AppendChild (docElement);
> 
> XmlElement fooElement = xmlDoc.CreateElement ("foo");
> docElement.AppendChild (fooElement);
> 
> This just fails with an ArguementException saying "Cannot use prefix
> with an empty namespace". That kind of tells me I'm not going in the
> right direction but I haven't had luck finding any proper examples.
> 
> I've seen the XmlNamespaceManager class and tried to use it. I can add
> namespaces but when I write out the document, they are not listed at
> all.
> 
> If anyone has any suggestions that would be great.
> 



More information about the Mono-list mailing list