XML 文档中有错误...调用 Web 服务时
我创建了一个 Web 服务和其中的一个函数,该函数应返回从通用数据库检索的 11000 条记录的列表,
这是我在 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;
}
}
然后,我在客户端程序中添加对此 Web 服务的 Web 引用,并调用引用 BBI。 然后我调用 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;
}
这是全部异常详细信息
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:
数据库记录包含各种奇怪的符号,例如¤rmann Kr。 Einarsson
和 Tv” 'fint∼ri
有人能看到我在这里做错了什么吗?
I have created a web service and a function in it that should return a list of 11thousand records retreived from a pervasive database
Here is my function in the web service.
[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.
Then i call to the getAll function and get the error : There is an error in XML document (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
and Tv” ‘fint˜ri
Can someone see what im doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 SoapUI 使用服务,发送请求,然后查看返回的内容。如果它在 SoapUI 中看起来不错,那么您就知道它正在从数据库中正确读取和提供,并且问题可能出在您的客户端上,可能与编码有关。如果 SoapUI 中看起来有问题,则表明服务器端或数据库中的数据有问题。
像 SoapUI 和 Fiddler 这样的工具非常适合对此类事情进行中间人检查。当您不确定问题出在服务器还是客户端时,将问题减少一半总是有帮助的。
Consume the service with SoapUI, send a request, and see what you get back. 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. If it looks wrong in SoapUI, it's something wrong at the server side or with the data in the database.
Tools like SoapUI and Fiddler are great, for man-in-the-middle inspections of this sort of thing. 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.
你的数据库里有那些奇怪的字符吗?如果是这样,则是数据库/客户端编码问题。调试服务器时能正确看到字符串吗?如果是这样,那就很奇怪了。无论如何,我想我记得如果有 \0 字符,那么这些 SOAP Web 服务喜欢抛出错误,但它在 WCF 中有效,但我重复一遍,我不确定。尽管如此,一开始就不应该有奇怪的角色。
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. Nevertheless, there shouldn't be strange characters in the first place.
您可以在这里找到答案:The quest for 0x0B
这是因为不允许使用某些字符在 XML 中,并且必须用转义实体替换或删除。正如上面的链接中提到的,它可能很难找到。
这是一些代码,如果它维护字符,则会重复问题:
我无法在此处显示无效字符,这是我所期望的,但您可以使用十六进制编辑器创建它们并将它们粘贴到代码中,然后替换它们与实体转义字符。
You'll find the answer here: The quest for 0x0B
This is because certain characters are not allowed in XML, and have to be replaced with escaped entities, or removed. 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:
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.
在 return 语句之前放置一个断点,并使用调试工具探索该列表。检查数据集合中是否有奇怪的符号。如果是这样,那么您需要转义这些特殊字符以便序列化响应。
查看 4.5 Framework 附带的 ASP.NET Web API,您可以更好地控制转换对客户端的响应的序列化器。
Put a Breakpoint right before your return statement and explore the list with the debugging tools. 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.