简体   繁体   English

XML文档中存在错误...调用Web服务时

[英]There is an error in XML document… When calling to web service

I have created a web service and a function in it that should return a list of 11thousand records retreived from a pervasive database 我已经创建了一个Web服务和一个函数,它应该返回从普适数据库中检索到的11,000条记录的列表

Here is my function in the web service. 这是我在Web服务中的功能。

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class BBI : System.Web.Services.WebService
{
    [WebMethod]
    public List<myObject> getAll()
    {
        List<myObject> result = new List<myObject>();
        PsqlConnection conn = new PsqlConnection("Host=soemthing;Port=something;Database=something;Encoding=IBM861");
        conn.Open();
        string strSql = "select 0, 1, 2, 3, 4, 5 from something";
        PsqlCommand DBCmd = new PsqlCommand(strSql, conn);
        PsqlDataReader myDataReader;
        myDataReader = DBCmd.ExecuteReader();
        while (myDataReader.Read())
        {
            myObject b = new myObject();
            b.0 = Convert.ToInt32(myDataReader[0].ToString());
            b.1 = myDataReader[1].ToString();
            b.2 = myDataReader[2].ToString();
            b.3 = myDataReader[3].ToString();
            b.4 = myDataReader[4].ToString();
            b.5 = myDataReader[5].ToString();
            result.Add(b); 
        }
        conn.Close();
        myDataReader.Close();
        return result;
    }
}

Then i add web reference to this web service in my client program and call the reference BBI. 然后我在我的客户端程序中添加对此Web服务的Web引用,并调用引用BBI。 Then i call to the getAll function and get the error : There is an error in XML document (1, 63432). 然后我调用getAll函数并得到错误:XML文档中存在错误(1,63432)。

public List<BBI.myObject> getAll()
{
    BBI.BBI bbi = new BBI.BBI();

    List<BBI.myObject> allBooks = bbi.getAll().OfType<BBI.myObject>().ToList(); 
    return allBooks;
}

Here is the total exception detail 这是完整的异常细节

System.InvalidOperationException was unhandled by user code
  Message=There is an error in XML document (1, 71897).
  Source=System.Xml
  StackTrace:
       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
       at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
       at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
       at BBI.BBI.getAllBooks() in c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\vefur\73db60db\a4ee31dd\App_WebReferences.jl1r8jv6.0.cs:line 252
       at webServiceFuncions.getAllBooks() in c:\Documents and Settings\forritari\Desktop\Vefur - Nýr\BBI\trunk\Vefur\App_Code\webServiceFuncions.cs:line 59
  InnerException: System.Xml.XmlException
       Message='', hexadecimal value 0x01, is an invalid character. Line 1, position 71897.
       Source=System.Xml
       LineNumber=1
       LinePosition=71897
       SourceUri=""
       StackTrace:
            at System.Xml.XmlTextReaderImpl.Throw(Exception e)
            at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
            at System.Xml.XmlTextReaderImpl.Throw(Int32 pos, String res, String[] args)
            at System.Xml.XmlTextReaderImpl.ParseNumericCharRefInline(Int32 startPos, Boolean expand, StringBuilder internalSubsetBuilder, Int32& charCount, EntityType& entityType)
            at System.Xml.XmlTextReaderImpl.ParseCharRefInline(Int32 startPos, Int32& charCount, EntityType& entityType)
            at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
            at System.Xml.XmlTextReaderImpl.ParseText()
            at System.Xml.XmlTextReaderImpl.ParseElementContent()
            at System.Xml.XmlTextReaderImpl.Read()
            at System.Xml.XmlTextReader.Read()
            at System.Xml.XmlReader.ReadElementString()
            at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderBBI.Read2_Book(Boolean isNullable, Boolean checkType)
            at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderBBI.Read20_getAllBooksResponse()
            at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer35.Deserialize(XmlSerializationReader reader)
            at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
       InnerException: 

The database records are containing all kind of strange symbols, for example ¤rmann Kr. Einarsson 数据库记录包含所有类型的奇怪符号,例如¤rmann Kr. Einarsson ¤rmann Kr. Einarsson and Tv” 'fint˜ri ¤rmann Kr. EinarssonTv” 'fint˜ri

Can someone see what im doing wrong here? 谁能看到我在这里做错了什么?

Consume the service with SoapUI, send a request, and see what you get back. 使用SoapUI消费服务,发送请求,看看你得到了什么。 If it looks good in SoapUI, then you know it's being read and fed from the database correctly, and the problem is likely with your client, probably with encoding. 如果它在SoapUI中看起来很好,那么你知道它正在从数据库正确读取和馈送,并且问题很可能在你的客户端,可能是编码。 If it looks wrong in SoapUI, it's something wrong at the server side or with the data in the database. 如果在SoapUI中看起来不对,那么服务器端或数据库中的数据就会出错。

Tools like SoapUI and Fiddler are great, for man-in-the-middle inspections of this sort of thing. 像SoapUI和Fiddler这样的工具非常棒,对于这种事情的中间人检查。 And when you aren't sure if it's a problem at the server or client, it's always helpful to cut the problem in half. 当您不确定服务器或客户端是否存在问题时,将问题减少一半总是有帮助的。

Does your database contain those strange characters? 您的数据库包含那些奇怪的字符吗? If so, it's a database/client encoding problem. 如果是这样,那就是数据库/客户端编码问题。 Can you see the strings correctly when you debug the server? 调试服务器时,您能正确看到字符串吗? If so, then it's very strange. 如果是这样,那就很奇怪了。 Anyway I think I remember that if there are \\0 characters, then these SOAP webservices like to throw errors, but it works in WCF, but I repeat, I'm not sure. 无论如何,我想我记得如果有0个字符,那么这些SOAP web服务就像抛出错误,但它在WCF中有效,但我再说一遍,我不确定。 Nevertheless, there shouldn't be strange characters in the first place. 尽管如此,首先应该没有奇怪的人物。

You'll find the answer here: The quest for 0x0B 你会在这里找到答案: 寻求0x0B

This is because certain characters are not allowed in XML, and have to be replaced with escaped entities, or removed. 这是因为XML中不允许使用某些字符,必须将其替换为转义实体或删除。 It can be tricky to find, as mentioned in the link above. 如上面的链接所述,找到它可能很棘手。

Here's some code, if it maintains the characters, that duplicates the issue: 这里有一些代码,如果它维护了字符,则会重复出现问题:

string xml = @"<title></title>";
var doc = new XmlDocument();

// Fails before escaping invalid chars.
try {
    doc.LoadXml(xml);
} catch(XmlException ex) {
    ex.Dump("Before");
}

// Works after escaping.
xml = xml.Replace("", "&#xB;");
xml = xml.Replace("", "&#x1;");
doc.LoadXml(xml);
doc.Dump("After");

I can't get the invalid characters to display here, which I expected, but you can create them with a hex editor and past them into your code, then replace them with the entity escape characters. 我无法在此处显示无效字符,这是我所期望的,但您可以使用十六进制编辑器创建它们并将它们放入代码中,然后将它们替换为实体转义字符。

Put a Breakpoint right before your return statement and explore the list with the debugging tools. 在return语句之前放置一个Breakpoint,并使用调试工具浏览列表。 Checkout if the data has the weird symbols inside the collection. 检查数据是否在集合中具有奇怪的符号。 If it does then you need to escape those special characters in order for the response to be serialized. 如果是,那么您需要转义这些特殊字符才能将响应序列化。

Check out the ASP.NET Web API that comes with the 4.5 Framework, you have more control of the serializer that transforms the response to clients. 查看4.5 Framework附带的ASP.NET Web API,您可以更好地控制将响应转换为客户端的序列化程序。

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

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