0

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Chetan
  • 443
  • 4
  • 8
  • What is the problem with these namespaces?? If they're never used in your XML, they shouldn't bother you.... – marc_s Apr 19 '10 at 12:03
  • The resultant xml is used by a third party and they want the xml output without the additional "frills" as they put it :) – Chetan Apr 19 '10 at 12:13
  • Those two namespaces are added by default - again: they don't cause any grief, so what's the problem?? Just ignore them, if you don't need them! – marc_s Apr 19 '10 at 14:01
  • Hi Marc, like I said, the resultant xml is going to be used by a third-party that is not using .Net or maybe they are parsing the xml manually..i have no say in that...so I have to figure out a way of removing the two namespace related tags... – Chetan Apr 19 '10 at 14:09
  • These things are not "frills", they're a standard part of any web service. – skaffman Apr 20 '10 at 08:59
  • If your third-party can't handle the "frills", then they don't process XML, and they need to be fixed. – John Saunders Apr 20 '10 at 09:08
  • @John: :)...doesn't work that way, I can't fix them..I have to give the output in the way they ask for..anyways found the solution... @skaffman: I know that, but all I am trying to do is to meet the clients requirement. – Chetan Apr 20 '10 at 11:40
  • @Chelan: It _does_ work that way with basics. They need to follow International standards. XML isn't XML if everyone interprets it their own way. How many different "versions" of XML do you want to support? – John Saunders Apr 20 '10 at 14:27

1 Answers1

1

I found the solution here, Returning XML natively in a .NET (C#) webservice?

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....

Community
  • 1
  • 1
Chetan
  • 443
  • 4
  • 8