Getting rid of an XML Namespace

  • Thread starter =?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno
  • Start date
?

=?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno

Hello.

I am creating a class that is derived from one that comes from an
auto-generated code from a webservice reference. The base class has
certain XML attributes that are inherited in my derived class and are
printed on the properties when trying to serialize the object. How can I
get rid of them in the derived class (without modifying the base class)?

Testcase:
using System;
using System.Collections.Generic;
using System.Text;

using System.Xml.Serialization;
using System.IO;

namespace XmlNamespaceProblem
{
[System.Xml.Serialization.XmlInclude(typeof(DerivedClass))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml",
"2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace =
"http://tempuri.org/TerminalUpdatesInformation.xsd")]
public class ClassFromWS
{
private string nameField;

private string descriptionField;

/// <remarks/>
public string Name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}

/// <remarks/>
public string Description
{
get
{
return this.descriptionField;
}
set
{
this.descriptionField = value;
}
}

}

public class DerivedClass : ClassFromWS
{
private int iNewField;

public int NewPropery
{
get { return this.iNewField; }
set { this.iNewField = value; }
}
}


public class Program
{

public static void Main()
{
DerivedClass oObj = new DerivedClass();
oObj.Description = "hello...";
oObj.Name = "this is my name";
oObj.NewPropery = 15;

XmlSerializer oSer = new XmlSerializer(typeof(ClassFromWS));
TextWriter oWriter = new StreamWriter("classfromws.xml");
oSer.Serialize(oWriter, oObj);
oWriter.Close();
}
}
}


Current results:
<?xml version="1.0" encoding="utf-8"?>
<ClassFromWS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="DerivedClass">
<Name xmlns="http://tempuri.org/TerminalUpdatesInformation.xsd">this
is my name</Name>
<Description
xmlns="http://tempuri.org/TerminalUpdatesInformation.xsd">hello...</Description>
<NewPropery>15</NewPropery>
</ClassFromWS>


Expected result:
<?xml version="1.0" encoding="utf-8"?>
<ClassFromWS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="DerivedClass">
<Name>this is my name</Name>
<Description>hello...</Description>
<NewPropery>15</NewPropery>
</ClassFromWS>


or


<?xml version="1.0" encoding="utf-8"?>
<ClassFromWS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="DerivedClass"
xmlns="http://tempuri.org/TerminalUpdatesInformation.xsd">
<Name>this is my name</Name>
<Description>hello...</Description>
<NewPropery>15</NewPropery>
</ClassFromWS>



Thanks in advance!

Regards,

Andrés [ knocte ]

--
 
S

sloan

This may not help, but I'll post it anyways.

http://sholliday.spaces.live.com/?_...842A!125&_c11_BlogPart_FullView=1&_c=BlogPart
9/21/2005 entry

I have several permutations of xml serializing. I don't know about your
exact issue, but I'll offer the code anyways.

But my output does not have your issue.

Ex:

<?xml version="1.0" encoding="utf-16"?><StateRootXmlNode
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<XmlStateAbbrev>NC</XmlStateAbbrev>
<XmlStateFullName>North Carolina</XmlStateFullName>
</StateRootXmlNode>




Andrés G. Aragoneses said:
Hello.

I am creating a class that is derived from one that comes from an
auto-generated code from a webservice reference. The base class has
certain XML attributes that are inherited in my derived class and are
printed on the properties when trying to serialize the object. How can I
get rid of them in the derived class (without modifying the base class)?

Testcase:
using System;
using System.Collections.Generic;
using System.Text;

using System.Xml.Serialization;
using System.IO;

namespace XmlNamespaceProblem
{
[System.Xml.Serialization.XmlInclude(typeof(DerivedClass))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml",
"2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace =
"http://tempuri.org/TerminalUpdatesInformation.xsd")]
public class ClassFromWS
{
private string nameField;

private string descriptionField;

/// <remarks/>
public string Name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}

/// <remarks/>
public string Description
{
get
{
return this.descriptionField;
}
set
{
this.descriptionField = value;
}
}

}

public class DerivedClass : ClassFromWS
{
private int iNewField;

public int NewPropery
{
get { return this.iNewField; }
set { this.iNewField = value; }
}
}


public class Program
{

public static void Main()
{
DerivedClass oObj = new DerivedClass();
oObj.Description = "hello...";
oObj.Name = "this is my name";
oObj.NewPropery = 15;

XmlSerializer oSer = new XmlSerializer(typeof(ClassFromWS));
TextWriter oWriter = new StreamWriter("classfromws.xml");
oSer.Serialize(oWriter, oObj);
oWriter.Close();
}
}
}


Current results:
<?xml version="1.0" encoding="utf-8"?>
<ClassFromWS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="DerivedClass">
<Name xmlns="http://tempuri.org/TerminalUpdatesInformation.xsd">this
is my name</Name>
<Description
xmlns="http://tempuri.org/TerminalUpdatesInformation.xsd">hello...</Descript
ion>
<NewPropery>15</NewPropery>
</ClassFromWS>


Expected result:
<?xml version="1.0" encoding="utf-8"?>
<ClassFromWS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="DerivedClass">
<Name>this is my name</Name>
<Description>hello...</Description>
<NewPropery>15</NewPropery>
</ClassFromWS>


or


<?xml version="1.0" encoding="utf-8"?>
<ClassFromWS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="DerivedClass"
xmlns="http://tempuri.org/TerminalUpdatesInformation.xsd">
<Name>this is my name</Name>
<Description>hello...</Description>
<NewPropery>15</NewPropery>
</ClassFromWS>



Thanks in advance!

Regards,

Andrés [ knocte ]

--
 
?

=?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno

sloan escribió:
This may not help, but I'll post it anyways.

It didn't help, sorry. Thanks anyway.

However after some tests now I have found a solution (using the second
overload of the ctor of XmlSerializer, to specify the global namespace).
I'll post the code:

using System;
using System.Collections.Generic;
using System.Text;

using System.Xml.Serialization;
using System.IO;

namespace XmlRootTest
{
[System.Xml.Serialization.XmlTypeAttribute(Namespace =
"http://tempuri.org/TerminalUpdatesInformation.xsd")]
public class ClassFromWS
{
private string nameField;

private string descriptionField;

/// <remarks/>
public string Name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}

/// <remarks/>
public string Description
{
get
{
return this.descriptionField;
}
set
{
this.descriptionField = value;
}
}

}

public class DerivedClass : ClassFromWS
{
private int iNewField;

public int NewPropery
{
get { return this.iNewField; }
set { this.iNewField = value; }
}
}


public class Program
{

public static void Main()
{
DerivedClass oObj = new DerivedClass();
oObj.Description = "hello...";
oObj.Name = "this is my name";
oObj.NewPropery = 15;

XmlSerializer oSer = new
XmlSerializer(typeof(DerivedClass),
"http://tempuri.org/TerminalUpdatesInformation.xsd");
TextWriter oWriter = new StreamWriter("classfromws.xml");
oSer.Serialize(oWriter, oObj);
oWriter.Close();
}
}
}


Regards,

Andrés [ knocte ]

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top