简体   繁体   English

.NET Web服务返回无效的xml

[英].NET web service returning invalid xml

I've got a simple .NET web service that is supposed to return a string value. 我有一个简单的.NET Web服务,应该返回一个字符串值。 It creates a DataSet and returns the GetXml() method, like so: 它创建一个DataSet并返回GetXml()方法,如下所示:

    [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: 问题是,当我从另一个程序调用此Web服务或以调试模式运行它并查看结果时,它为我提供了以下内容:

<?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. 我要做的就是返回该结果的xml,以便可以在未使用.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. 我正在编写一个使用此Web服务的Android应用程序。 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. 我可以很好地连接和检索xml字符串,但是当我尝试解析它时,由于字符串中有多余的空间,它会丢失节点。 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. 为了进一步确认这一点,如果我直接将Web服务URL中的输出复制并粘贴到一个空文件中,将其另存为.xml并尝试对其进行查看,则它不会打开。 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. 另外,当我尝试右键单击Web服务输出页面以“查看源代码”时,它说目前无法查看它。 However, when I use a correctly formatted xml string, I can view it in a browser and view the source. 但是,当我使用格式正确的xml字符串时,可以在浏览器中查看它并查看源。

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. 不太确定在这里还有什么要说的...。我们已经在Android / Java方面做了很多故障排除,在这里是StackOverflow Android问题,以及当我们使用上面格式化的XML时(意味着其中有多余的空格) ),解析器不会选择IssueSummary元素。 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. 但是,如果我手动删除空格并将应用程序指向新文件(而不是Web服务),则它将找到IssueSummary元素并正常显示数据。 Doesn't that lead you to believe it's got something to do with the spaces between the nodes? 难道这不使您相信它与节点之间的空间有关吗?

UPDATE #2 - Resolution 更新#2-解决

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. 我们的网络方法使用的是.GetXml(),它返回上面的Xml,每个节点之间有空格。 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: 我和我的同事使用Web服务尝试了几件事,并且弄清楚了是否我们使用XmlDocument并调用InnerXml属性,它将返回不带这些空格的xml:

    [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. 完成此操作后,另一端(Android App)的解析器将对其进行完美处理。 So it appears safer to use .InnerXml of an XmlDocument instead of the .GetXml() of a DataSet. 因此,使用XmlDocument的.InnerXml而不是DataSet的.GetXml()似乎更安全。

Thanks to those who have helped, very much appreciated! 感谢那些人的帮助,非常感谢!

I think the formatting is trivial. 我认为格式很简单。 The xml parser will parse the document as xml regardless if there is a space between them. xml解析器会将文档解析为xml,无论它们之间是否有空格。 Calls a function, gets an xml, and xml reader processes the xml. 调用一个函数,获取一个xml,然后xml阅读器处理该xml。 Maybe the document itself has a space. 也许文档本身有一个空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM