Groups | Blog | Home
all groups > dotnet xml > january 2004 >

dotnet xml : Form and XmlRootElement


Richard Grimes
1/30/2004 3:12:57 PM
I have an XML file with no namespaces defined:

<data>
<textItems>
<add name="test" value="test value"/>
</textItems>
</data>

I can map this to a class with:

[XmlRoot]
public class data
{
[XmlArray("textItems", Form=XmlSchemaFor.Unqualified)]
[XmlArrayItem("add", typeof(addItem), Form=XmlSchemaFor.Unqualified)]
public addItem[] Items;
}

public class addItem
{
[XmlAttribute] public string name;
[XmlAttribute] public string value;
}

That is fine. I can read in the XML file, deserialize it with XmlSerializer
to a data object and access the Items. I can alter the Items array and write
the data object to the XML file by serializing it.

However, I find that the XmlSerializer writes the root element with
xmlns:xsd and xmlns:xsi attributes:

<data xmlns:xsd="stuff" xmlns:xsi="stuff">
<!-- stuff -->
</data>

How do I prevent that? [XmlRoot] does not have a Form property.

Richard
--
my email evpuneqt@zicf.bet is encrypted with ROT13 (www.rot13.org)

Daniel Cazzulino
1/30/2004 3:21:33 PM
You can avoid both namespace declarations by using a custom XmlTextWriter.
Here's one such writer:
http://weblogs.asp.net/cazzu/archive/2004/01/23/62141.aspx. Look at the
NonXsiTextWriter.


--
Daniel Cazzulino
Lagash Systems SA
http://weblogs.asp.net/cazzu

[quoted text, click to view]

Richard Grimes [MVP]
1/30/2004 4:09:29 PM
[quoted text, click to view]
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=e4wQER%24MCHA.2428%40tkmsftngp12

Oleg,

Its not really my expertise, so please excuse my ignorance <g>

But I have to admit that adding empty namespaces is hardly logical, but it
works. Thanks!

Richard
--
my email evpuneqt@zicf.bet is encrypted with ROT13 (www.rot13.org)

Oleg Tkachenko [MVP]
1/30/2004 5:30:00 PM
[quoted text, click to view]

What's wrong with them?

Anyway, it's a faq:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=e4wQER%24MCHA.2428%40tkmsftngp12

--
Oleg Tkachenko [XML MVP, XmlInsider]
AddThis Social Bookmark Button