By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 1,942 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

Getting rid of an XML Namespace

P: n/a
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(Derived Class))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("Sy stem.Xml",
"2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlTypeAttribute(Namespac e =
"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 ]

--
Sep 25 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

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

http://sholliday.spaces.live.com/?_c11_BlogPart_n=1&_c11_BlogPart_handle=cns!A68482 B9628A842A!125&_c11_BlogPart_FullView=1&_c=BlogPar t
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 [ knocte ]" <kn****@NO-SPAM-PLEASE-gmail.comwrote in
message news:uH****************@TK2MSFTNGP04.phx.gbl...
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(Derived Class))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("Sy stem.Xml",
"2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlTypeAttribute(Namespac e =
"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 ]

--

Sep 25 '06 #2

P: n/a
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(Namespac e =
"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 ]

--
Sep 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.