471,610 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

serializing element text using SOAP

I have generated a proxy class derived from
System.Web.Services.Protocols.SoapHttpClientProtoc ol

One of my classes is defined as

[System.Xml.Serialization.SoapTypeAttribute("MyClas s",
"http://www.mycompany.com")]
public class MyClass
{
public MyClass()
{
}

public MyClass(String text)
{
this.Value = text;
}

[XmlText(typeof(string))]
public string Value;
}

I wish for instances of this class to be serialized as

<tns:MyClass>hello world</tns:MyClass>

However it serializes as

<tns:MyClass><Value xsi:type="xsd:string">hello
world</Value></tns:MyClass>

I guess my use of XMLText() here doesn't work. Do anyone know how to
properly serialize element text?
Nov 12 '05 #1
1 1685
I suspect that is because you are using SOAP Serialization, not XML
Serialization.
Do you by chance have a SoapDocumentService or SoapRpcService attribute on
your code?

see
http://msdn.microsoft.com/library/en...iceclients.asp

The following demonstrates Xml Serialization. It generates the xml you want
( <MyClass>hello</MyClass> ).

To get this implicitly with webservices, you need to use decorate your
webmethod with
Use:=SoapBindingUse.Literal

using System.Xml.Serialization;

namespace Ionic {

public class MyClass
{
public MyClass() { }

public MyClass(string text)
{
this.Value = text;
}

[XmlText(typeof(string))]
public string Value;
}

/// XmlTextWriterFormattedNoDeclaration
/// helper class
public class XmlTextWriterFormattedNoDeclaration :
System.Xml.XmlTextWriter {
public XmlTextWriterFormattedNoDeclaration (System.IO.TextWriter w) :
base(w) { Formatting= System.Xml.Formatting.Indented;}
public override void WriteStartDocument () { }
}
public class TestDriver {

static void Main(string[] args) {
XmlSerializer s = new XmlSerializer(typeof(MyClass));

// use this to "suppress" the default xsd and xsd-instance namespace's
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add( "", "" );

MyClass c= new MyClass("hello");

// spit it out
System.Console.WriteLine("\nMyClass Serialized:");
s.Serialize(new
XmlTextWriterFormattedNoDeclaration(System.Console .Out), c, ns);
System.Console.WriteLine("\n");

}
}
}
"Lena" <ch********@hotmail.com> wrote in message
news:3a**************************@posting.google.c om...
I have generated a proxy class derived from
System.Web.Services.Protocols.SoapHttpClientProtoc ol

One of my classes is defined as

[System.Xml.Serialization.SoapTypeAttribute("MyClas s",
"http://www.mycompany.com")]
public class MyClass
{
public MyClass()
{
}

public MyClass(String text)
{
this.Value = text;
}

[XmlText(typeof(string))]
public string Value;
}

I wish for instances of this class to be serialized as

<tns:MyClass>hello world</tns:MyClass>

However it serializes as

<tns:MyClass><Value xsi:type="xsd:string">hello
world</Value></tns:MyClass>

I guess my use of XMLText() here doesn't work. Do anyone know how to
properly serialize element text?

Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Scott Meddows | last post: by
2 posts views Thread by Tobias Zimmergren | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
1 post views Thread by ZEDKYRIE | last post: by

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.