Robert
Robert

Reputation: 1726

.NET web service returning invalid xml

I've got a simple .NET web service that is supposed to return a string value. It creates a DataSet and returns the GetXml() method, like so:

    [WebMethod]
    public string GetOpenIssues(string CustomerCode, string LocationShortName)
    {
        DAL.EncodedConnectionString = ConfigurationManager.ConnectionStrings["EncodedConnectionString"].ConnectionString;
        System.Data.SqlClient.SqlCommand sc = new System.Data.SqlClient.SqlCommand("GetOpenIssues");
        sc.Parameters.AddWithValue("CustomerCode", CustomerCode);
        sc.Parameters.AddWithValue("LocationShortName", LocationShortName);
        return DAL.SPDataSet(sc, "OpenIssues").GetXml();
    }

The problem is when I call this web service from either another program or run it in debug mode and view the results, it gives me the following:

<?xml version="1.0" encoding="utf-8" ?> 
<string xmlns="RemoteWebService"><OpenIssues> <Table> <IssueID>15351</IssueID>   <IssueSummary>Computer keeps crashing. This continues to be a problem</IssueSummary> <LocationName>West Side</LocationName> <Status>WIP</Status> <CustomerID>89755</CustomerID> <CustomerName>West Side Computers</CustomerName> <CustomerShortName>WSC</CustomerShortName> <Notes /> <STATUS1>Work In Progress</STATUS1> <SubmittedBy>WSC - Tom Johns</SubmittedBy> <EQ_Replaced>true</EQ_Replaced></Table> </OpenIssues></string> 

Which, as you can tell, is not properly formatted. For some reason it's putting spaces in between each element.

Any ideas as to what I'm doing wrong? All I want to do is return the xml for this result so I can use it in other in-house apps that aren't written in .NET.

UPDATE Thanks for the replies but I believe formatting is an issue and here's why. I'm writting an Android app that consumes this web service. I can connect and retrieve the xml string just fine but when I try to parse it, it misses the node because of the extra space in the string. To confirm this even more, if I copy and paste the output directly from the web service URL, into an empty file, save it as .xml and try to view it, it doesn't open. Also, when I try to right-click on the web service output page to "view source", it says it can't be viewed at this time. However, when I use a correctly formatted xml string, I can view it in a browser and view the source.

Not too sure what else to say here....We've done a lot of troubleshooting on the Android/Java side of things, here StackOverflow Android Question and when we use the XML as formatted above (meaning with the extra spaces in it), the parser doesn't pick up the IssueSummary element. However, if I manually remove the spaces and point the app to the new file (instead of the web service), then it finds the IssueSummary element and displays the data fine. Doesn't that lead you to believe it's got something to do with the spaces between the nodes?

UPDATE #2 - Resolution

After digging and digging I was able to figure out a resolution. Our web method was using .GetXml() which was returning the Xml above with spaces between each node. My coworker and I tried several things with the web service and figured out if we use an XmlDocument and call the InnerXml property instead, it returns the xml without those spaces:

    [WebMethod]
    public string GetOpenIssues(string CustomerCode, string LocationShortName)
    {
        DAL.EncodedConnectionString = ConfigurationManager.ConnectionStrings["EncodedConnectionString"].ConnectionString;
        System.Data.SqlClient.SqlCommand sc = new System.Data.SqlClient.SqlCommand("GetOpenIssuesSite");
        sc.Parameters.AddWithValue("CustomerCode", CustomerCode);
        sc.Parameters.AddWithValue("ICAO", LocationShortName);
        System.Data.DataSet ds = DAL.SPDataSet(sc, "OpenIssues");

        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        System.IO.StringWriter sw = new System.IO.StringWriter();
        ds.WriteXml(sw);

        doc.LoadXml(sw.ToString());

        return doc.InnerXml;
    }

Once we did this, the parser on the other end (Android App) processed it perfectly. So it appears safer to use .InnerXml of an XmlDocument instead of the .GetXml() of a DataSet.

Thanks to those who have helped, very much appreciated!

Upvotes: 1

Views: 1439

Answers (1)

iefpw
iefpw

Reputation: 7062

I think the formatting is trivial. The xml parser will parse the document as xml regardless if there is a space between them. Calls a function, gets an xml, and xml reader processes the xml. Maybe the document itself has a space.

Upvotes: 4

Related Questions