views:

118

answers:

1

HI, This is an old question, i have seen some solutions on this forum itself, but am trying to use webservices for the first time so please bear with me on this one.

I have a webservice that returns XML in the following format

<subs xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" msisdn="965xxxxxx">
<shortcode label="XXXX">
<channels>
<channel>
<id>442</id>
<name>News</name>
<billingperiod>7</billingperiod>
<billingamount>3</billingamount>
<lastbilling>4/14/2010 1:41:11 PM</lastbilling>
</channel>
<channel>
<id>443</id>
<name>News2</name>
<billingperiod>7</billingperiod>
<billingamount>3</billingamount>
<lastbilling>4/14/2010 1:41:19 PM</lastbilling>
</channel>
</channels>
</shortcode>
</subs>

I want the same XMl output without the xmlns:xsd and xmlns:xsi tags.

I have tried the following solution that was suggested:

  <WebMethod(MessageName:="GetSubscription")> 
  Public Function GetSubscription(....) As String

  Dim namespaces As New XmlSerializerNamespaces
  namespaces.Add(String.Empty, String.Empty)

  Dim serializer As New XmlSerializer(SubsDetail.GetType)
  Dim sw As New System.IO.StringWriter
  Dim writer As New System.Xml.XmlTextWriter(sw)
  writer.Formatting = Formatting.None
  serializer.Serialize(writer, SubsDetail, namespaces)
  writer.Close()
  Return sw.toString()

The result was that I got an xml in the following format:

<string>
<?xml version="1.0" encoding="utf-16"?><subs msisdn="965xxxx">
    <shortcode label="XXXX">
    <channels>
    <channel>
    <id>442</id>
    <name>News</name>
    <billingperiod>7</billingperiod>
    <billingamount>3</billingamount>
    <lastbilling>4/14/2010 1:41:11 PM</lastbilling>
    </channel>
    <channel>
    <id>443</id>
    <name>News2</name>
    <billingperiod>7</billingperiod>
    <billingamount>3</billingamount>
    <lastbilling>4/14/2010 1:41:19 PM</lastbilling>
    </channel>
    </channels>
    </shortcode>
    </subs>
</string>

Though the format of the xml is correct it is coming as string within the <string> tags. This is really driving me nuts.

Can I get the output as xml without the outer string tags?

A: 

Hi, I found the solution here, http://stackoverflow.com/questions/496222/returning-xml-natively-in-a-net-c-webservice/496891#496891

I didn't use it in exactly the same way though, I serialized the class using attribute tags for the members and then loaded the xml string into an XMl Document and returned it to the calling service...

Here is a sample of the code:

  Dim namespaces As New XmlSerializerNamespaces
  namespaces.Add(String.Empty, String.Empty)

  Dim serializer As New XmlSerializer(SubsDetail.GetType)
  Dim sw As New System.IO.StringWriter
  Dim writer As New System.Xml.XmlTextWriter(sw)
  'writer.Formatting = Formatting.None
  serializer.Serialize(writer, SubsDetail, namespaces)
  writer.Flush()
  Dim xmlDocument As New XmlDocument
  xmlDocument.LoadXml(sw.ToString())
  Return xmlDocument

My Struct construct that I serialize uses the Xml attributes in the following way:

  <XmlRootAttribute("subs", _
 Namespace:="", IsNullable:=False)> _
  Structure Subs
    <XmlAttributeAttribute()> _
    Public msisdn As String
    <XmlElement()> _
    Public shortcode() As shortcodelist
    '<XmlNamespaceDeclarations()> _
    'Public xmlns As XmlSerializerNamespaces

  End Structure

Hope this helps somebody..somewhere..sometime....

Chetan