473,320 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Getting rid of an XML Namespace

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
2 2729

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Don | last post by:
Hi: I have created an xsd from my xml document. I pop this xsd in the following directory: C:\program files\Microsoft Visual Studio .NET 2003\Common7\Packages\schemas\xml. That give me...
4
by: Richard | last post by:
Hi All, I am using Visual C++ .Net to create a windows forms application and I am getting the following errors when I do a MessageBox::Show in my Form1.cpp file: Form1.cpp(19): error C2653:...
5
by: Ken Varn | last post by:
The following code snippet will not compile. I get an error C2337 saying that the flags attribute is not found, however, if I remove the last namespace System from the namespace tree, then it...
8
by: Charles | last post by:
I do not understand why I am getting a "Specified cast is not valid" error, since it has worked before. Something has changed and I am not really sure what it could be. I am looking for something...
6
by: Erik H. | last post by:
Trying to connect to MySQL db on localhost, and populate datagrid from a dataset using code inline method. Getting the following compile error: Error Message: "CS0246: The type or namespace...
2
by: Danny Gagne | last post by:
I'm currently working an .net application (I can use 1.1 or 2.0 if needed) that needs to read a wsdl file and generate another piece of code that can use it. I'm encountering a problem where I...
1
by: Paul J. Lucas | last post by:
I have code that builds a DOM using the Java org.w3c.dom API and it adds an extra attribute to one of the elements that I don't want. Not only that, the value it adds is wrong. The code to...
33
by: JamesB | last post by:
I am writing a service that monitors when a particular app is started. Works, but I need to get the user who is currently logged in, and of course Environment.UserName returns the service logon...
3
by: tshad | last post by:
I have a file that I converted from VB.Net to C# that works fine in VB.Net when I compile but not in C# using the same libraries. The error I am getting is: PageInit.cs(9,7): error CS0138: A...
5
by: tshad | last post by:
I have the following class in my VS 2008 project that has a namespace of MyFunctions. ********************************* Imports System Imports System.Text.RegularExpressions Namespace...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.