Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: XML Namespace Prefix

From: "Techno_Dex" <nospamchurst@--------.--->
To: 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;
>     }
> }
> 




transparent
Print
Mail
Digg
delicious
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent