XML Namespace Prefix - XML SOAP
This is a discussion on XML Namespace Prefix - XML SOAP ; 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
...
-
XML Namespace Prefix
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;
}
}
-
Re: XML Namespace Prefix
Techno_Dex wrote:
> 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.
This overload of the Serialize method
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlSerializationXmlSerializerClassSerializeTopic5.asp>
takes a third argument where you can pass in an object mapping prefixes
to namespace URLs so that the serializer uses the prefixes.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
-
Re: XML Namespace Prefix
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@osi-corp.com> wrote in message
news:%23PEIX5d$GHA.2256@TK2MSFTNGP03.phx.gbl...
> 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;
> }
> }
>
Similar Threads
-
By Application Development in forum XML SOAP
Replies: 2
Last Post: 07-12-2007, 10:45 AM
-
By Application Development in forum XML SOAP
Replies: 1
Last Post: 06-05-2007, 03:14 AM
-
By Application Development in forum DOTNET
Replies: 0
Last Post: 12-28-2006, 05:27 PM
-
By Application Development in forum DOTNET
Replies: 1
Last Post: 10-16-2006, 12:23 PM
-
By Application Development in forum XML SOAP
Replies: 2
Last Post: 06-27-2005, 09:34 AM