Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 11th, 2006, 03:55 PM
Rahul
Guest
 
Posts: n/a
Default XmlSerializerNamespaces not working the same in dotnet 2.0 as

Hi,

I am having trouble serializing an object in dotnet 2.0.

I am using theis method

public static string Serialize(object o, string defaultNamespace)
{
//create empty namespace to prevent namespace output
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", defaultNamespace);
//create serializer
XmlSerializer ser = new XmlSerializer(o.GetType());

using(StringWriter writer = new StringWriter())
{
//use custom writer to prevent root 'xml' tag output
XmlNoPrefixTextWriter xwriter = new XmlNoPrefixTextWriter(writer);
xwriter.Formatting = Formatting.None;

ser.Serialize(xwriter, o, ns);
return writer.ToString();
}
}



In dotnet 1.1. the output I get is <TestDTO
xmlns="http://House/Test"><X>Hi</X><Y>There</Y></TestDTO>

In dot net 2.1 the output I get is <TestDTO><X>Hi</X><Y>There</Y></TestDTO>



Basically I am missing the namespace reference in the xml. Is there a way to
fix this. What changes do I need to make.



Thanks

Rahul Aggarwal


  #2  
Old August 11th, 2006, 05:05 PM
Jiho Han
Guest
 
Posts: n/a
Default Re: XmlSerializerNamespaces not working the same in dotnet 2.0 as

Hello Rahul,

What does that XmlNoPrefixTextWriter do?
Quote:
Hi,
>
I am having trouble serializing an object in dotnet 2.0.
>
I am using theis method
>
public static string Serialize(object o, string defaultNamespace)
{
//create empty namespace to prevent namespace output
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", defaultNamespace);
//create serializer
XmlSerializer ser = new XmlSerializer(o.GetType());
using(StringWriter writer = new StringWriter())
{
//use custom writer to prevent root 'xml' tag output
XmlNoPrefixTextWriter xwriter = new XmlNoPrefixTextWriter(writer);
xwriter.Formatting = Formatting.None;
ser.Serialize(xwriter, o, ns);
return writer.ToString();
}
}
In dotnet 1.1. the output I get is <TestDTO
xmlns="http://House/Test"><X>Hi</X><Y>There</Y></TestDTO>
>
In dot net 2.1 the output I get is
<TestDTO><X>Hi</X><Y>There</Y></TestDTO>
>
Basically I am missing the namespace reference in the xml. Is there a
way to fix this. What changes do I need to make.
>
Thanks
>
Rahul Aggarwal
>

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles