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

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?

Answers


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.


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.


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:

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


Need Your Help

WPF TreeView does not apply DataTemplate accordingly

c# wpf templates treeview

I have a business object project, which contains composite structure:

Add custom style property to MXML Custom Component

css flex flex3 components mxml

I have a Custom Component that has a couple of Canvas with some background colors assigned to them. Now i have hard coded the colors, i want to move them to an external css file.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.