Click to See Complete Forum and Search --> : XML Excepiton in C#


Akademos
June 12th, 2003, 03:35 AM
Hi,
I've got following problem: I have a MFC-Programm which has a WebBrowser. The WebBrowser navigates to a WebApplication with C# as CodeBehind. Some XML-Data will be sended to the WebApplication as POST-Data. When i try to load the post data into a XML Stream I've got a Exception with following message:

An unexpected end of file parsing NAME has occurred.
Line 1, position 2.

I'don't have any idea what could causes this exception because the xml-syntax is correct.

Here some pieces of code:

On the MFC-Site. Construct the XML and call Webbrowser:

spDoc->save( L"c:\\report_response.xml" );
MSXML2::IXMLDOMDocument2Ptr spTmpDoc;
HRESULT hr = spTmpDoc.CreateInstance( __uuidof( MSXML2::DOMDocument40 ) );
WgString sXML = L"<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><selection>";

try
{
//Read owner
MSXML2::IXMLDOMNodePtr spOwner = spDoc->selectSingleNode( L"//owner" );


sXML += (wchar_t *)spOwner->xml;



MSXML2::IXMLDOMNodePtr spReport = spDoc->selectSingleNode( L"//report" );
sXML += (wchar_t *)spReport->xml;

sXML += L"</selection>";
spTmpDoc->loadXML( sXML.U( ) );
}
catch( _com_error e)
{
TRACE( L"%s\n", (wchar_t *) e.Description( ));
}

spTmpDoc->save( L"C:\\webbrowser.xml" );
m_edtInfo.ShowWindow( SW_HIDE );
CRect rect;
m_edtInfo.GetWindowRect( &rect );
WgString sRequest = spTmpDoc->xml;
TRACE( L"%s\n", sRequest.U( ) );
wchar_t *pwcRequest = sRequest;


CComVariant postData( pwcRequest /*L"Owner=me_myself&report=1column.rpt"*/ );
wchar_t *sPostDataString = pwcRequest ;
int len = wcslen( sPostDataString );
postData.vt = VT_ARRAY;
postData.parray = SafeArrayCreateVector(VT_UI2, 0, len);
void HUGEP* safeData;
hr = SafeArrayAccessData(postData.parray, &safeData);
wmemcpy((wchar_t *)safeData, sPostDataString, len);
hr = SafeArrayUnaccessData(postData.parray);

(&CWnd::wndTop,rect.left,rect.top,rect.Width(), rect.Height(),SWP_SHOWWINDOW );
m_search.Navigate( L"http://localhost/RDCASPNET/WebForm1.aspx", NULL, NULL, &postData, NULL );


The XML Doc which will be sended as POST-Data:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<selection><owner>
me_myself
</owner><report>
5Columns.rpt
</report></selection>


The C# code at the web application:

byte[] arrByte = Context.Request.BinaryRead( Context.Request.ContentLength );

String sPostData = Context.Request.ContentEncoding.GetString( arrByte ) + "\n";
TextBox1.Text += sPostData;

System.Xml.XmlDocument doc = new System.Xml.XmlDocument( );

try
{
doc.LoadXml( sPostData );
}
catch( Exception ex )
{
TextBox1.Text += "doc.LoadXml" + ex.Message + ex.Source;
}


Does anyone knows what could have gone wrong?

Thanks in Advance

Markus

newvisva
June 12th, 2003, 10:41 AM
I suspect it could be the problem with ur xml. Reconstruct ur xml with the following below. What I've done is just removing the enter keys to let ur reader to read.


<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<selection>
<owner>me_myself</owner>
<report>5Columns.rpt</report>
</selection>

Akademos
June 13th, 2003, 06:19 AM
No, if found that it has to do with the encoding. When i do something like that:

String sPostData = System.Text.ASCIIEncoding.Unicode.GetString( arrByte );

doc.loadXML(sPostData);


It works fine. But i don't know what will happen when no-latin charakters (like Kanji or Hiragana) will occur in the XML-Stream.

Akademos