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

dotnet xml : Avoiding XML serialization namespace substitution?


AP
4/27/2004 2:46:07 PM
Hi,

I have a class that I wish to serialize to XML, part of which looks like
this:


public class TitleNotification {
[XmlAttributeAttribute(Namespace="xsi",
AttributeName="noNameSpaceSchemaLocation")]
public string NoNameSpaceSchemaLocation =
http://www.pdr.com\nhttps://extw3c.pdr.com/prism/b2b/schemas/TitleNotification.xsd;
....
}

When the class is serialized, the relevant resulting XML looks like this:

<?xml version="1.0" encoding="utf-8"?>
<TitleNotification xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
d1p1:noNameSpaceSchemaLocation="http://www.pdr.com&#xA;https://extw3c.pdr.co
m/prism/b2b/schemas/TitleNotification.xsd" ParagonID="1234" Version="1"
Source="5" SupplierID="PA-1234" xmlns:d1p1="xsi" ...

How can I get it to generate xsi:noNameSpaceSchemaLocation instead of doing
the d1p1 substitution thing it's doing now?

Thanks,

Adam

Derek Harmon
4/27/2004 8:24:01 PM
[quoted text, click to view]

The Namespace property represents a namespace URI -- not a prefix.

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

Also observe that the QName, xsi:noNamespaceSchemaLocation, is
case-sensitive.

: :
[quoted text, click to view]

Try using the namespace URI, XmlSerializer may re-use the xsi prefix.
If it doesn't, then it's choice of prefix is arbitrary. Correct implementations
that honor xsi:noNamespaceSchemaLocation are actually looking for:

{http://www.w3.org/2001/XMLSchema-instance,noNamespaceSchemaLocation}

Therefore, it doesn't matter what prefix it's given.

Ordinarily, you would use a public field of type XmlSerializerNamespaces,
and mark it with the [XmlNamespaceDeclarations] attribute. You could
initialize this field by Add'ing prefix and namespaceURI pairings into it.

However, the XML Schema namespace declarations are emitted by the
XmlSerializer prior to consulting the XmlNamespaceDeclarations field,
so you cannot count on your xsi namespace declaration appearing early
enough in the root element (it'll be there, though, because XmlSerializer
needs the XMLSchema-instance namespace itself.)


Derek Harmon

AP
4/28/2004 10:51:41 AM
So in other words there's no gauranteed way of doing it?

[quoted text, click to view]

Daniel Cazzulino [MVP XML]
4/28/2004 7:48:49 PM
You can get rid of the xsd and xsi namespace declarations by using the
technique explained here:
http://weblogs.asp.net/cazzu/archive/2004/01/23/62141.aspx, at the very
beginning of the post.

HTH
--
Daniel Cazzulino [MVP XML]
Clarius Consulting SA
http://weblogs.asp.net/cazzu
http://aspnet2.com
[quoted text, click to view]


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.665 / Virus Database: 428 - Release Date: 23/04/2004

Daniel Cazzulino [MVP XML]
4/28/2004 8:54:15 PM
Well, not quite what you wanted anyway :S

--
Daniel Cazzulino [MVP XML]
Clarius Consulting SA
http://weblogs.asp.net/cazzu
http://aspnet2.com
[quoted text, click to view]


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.665 / Virus Database: 428 - Release Date: 23/04/2004

AddThis Social Bookmark Button