![]() |
![]() | ![]() | ![]() | Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - XML Namespace Prefix [Thread Next] Re: XML Namespace PrefixTo: NULL Date: 11/1/2006 12:49:00 PM
As a followup to this point, I have discovered how the get Namespace
Prefixes add......
When serializing a class using an XmlSerializer, one of the parameters that
can be passed in is an object containing namespaces. This is what will
modify the XML output to use the appropriate Namespace prefixes.
SerializableObjectFoo oSerializableObjectFoo;
XmlSerializer xsSerializer;
TextWriter twWriter;
XmlSerializerNamespacew xsnNamespaces;
oSerializableObjectFoo = new SerializableObjectFoo():
oSerializableObjectFoo.ElementName.Value1 = "A";
oSerializableObjectFoo.ElementName.Value2 = "B";
xsnNamespaces = new XmlSerializerNamespaces();
xsnNamespaces.Add("", "http://www.w3.org/schemas");
xsnNamespaces.Add("rex", "http://www.rex.org/schemas");
xsnNamespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
twWriter = new StreamWriter(@"C:\Temp\Export.xml");
xsSerializer.Serialize(twWriter, oSerializableObjectFoo, xsnNamespaces);
twWriter.Close();
***********************
Then the class will look something like
***********************
[Serializable]
[XmlElementAttribute(ElementName="Foo", IsNullable=true,
Form=XmlSchemaForm.Qualified)]
public class Foo
{
[XmlElementAttribute(ElementName="ElementName",
Namespace="http://www.rex.org/schemas")]
public ElementName ElementName
{
get {return menElementName;}
set {menElementName = value;}
}
}
[XmlRoot(ElementName="ElementName", Namespace =
"http://www.rex.org/schemas")]
[XmlType(TypeName="elementNameType", Namespace =
"http://www.rex.org/schemas")]
public class ElementName
{
[XmlElement("Value1")]
public string Value1
{
get;
set;
}
[XmlElement("Value2")]
public string Value2
{
get;
set;
}
}
If you only declare the XmlRoot Attribute on the ElementName class, then
only the children will get the Namespace prefix. The ElementName property
in the Foo Class needs to namespace also so it is explicitly declared. Make
sure the Namespace= "<Value>" matches exactly as what is put in the
XmlSerializableNamespaces collection otherwise the Serializer won't
recognize the item and prefix the Element with the Namespace prefix.
"Techno_Dex" <nospamchurst@o...> wrote in message
news:%23PEIX5d$GHA.2256@T......
> Can anyone tell me how to add an XMLNamespace prefix to a C# class
> declaration? I have a couple of different namespaces that must be
> declared in my XML file but I can't figure out what class/property
> attributes I need to add to have a namespace prefix generated in front of
> the element when the class is serialized.
>
> I am looking for "rex" so the serialization will occur like the following.
> <rex:ElementName>
> <rex:Value1> A</rex:Value1>
> <rex:Value2> B</rex:Value2>
> </rex:ElementName>
>
> So far all I am able to come up with is
> <ElementName xmlns="rex">
> <Value1 xmlns="rex"> A</Value1>
> <Value2 xmlns="rex"> B</Value2>
> </rex:ElementName >
>
> I get this when I have the following
> [XmlElementAttribute(ElementName="Foo", IsNullable=true,
> Form=XmlSchemaForm.Qualified)]
> public class Foo
> {
> [XmlElement("ElementName")]
> public ElementName ElementName
> {
> get {return menElementName;}
> set {menElementName = value;}
> }
> }
>
> public class ElementName
> {
> [XmlElement("Value1")]
> public string Value1
> {
> get;
> set;
> }
> [XmlElement("Value2")]
> public string Value2
> {
> get;
> set;
> }
> }
>
| ![]() | ![]() | ![]() |
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | |||||
|
