简体   繁体   中英

Exact way to receive an XML by C# Web service

I appreciate your help.

I have created a web service to receive an XML file, so I followed the below approach then I published it and it worked fine for me : ....

 XmlDocument xmldoc = new XmlDocument();
try
{
    if (HttpContext.Current.Request.InputStream != null)
    {
        StreamReader stream = new  StreamReader(HttpContext.Current.Request.InputStream);
        string xmls = stream.ReadToEnd();
        xmldoc.LoadXml(xmls);
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.ValidationType = ValidationType.Schema;
    }

}
catch (Exception ex)
{
    logger.Log(NLog.LogLevel.Error, ex.Message + ex.StackTrace);
}

...

knowing that my XML structure is:

<reports uis="5521452542">
  <attribute1>val1</attribute1>
  ...
</reports>

but after testing by some friends, that called my web service from the Lunix platform I received in the Log file error the below message error; knowing that their XML file is validated.

Just to let you know; that their XML file did not contains the declaration of:

<?xml version="1.0" encoding="UTF-8"?> 

Can this provide the error or NOT ?

2014-04-03 03:56:53.7408|Error|Root element is missing.
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
   at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
   at System.Xml.XmlDocument.Load(XmlReader reader)
   at System.Xml.XmlDocument.LoadXml(String xml)
   at WebService.Service1.GetInfoService() in  
   D:\yassine\Mobily\Log\WebService\WebService\WebService\Service1.asmx.cs:line 56
   2014-04-03 03:56:53.8032|Error|Root element is missing.   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
   at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
   at System.Xml.Linq.XDocument.Parse(String text, LoadOptions options)
   at WebService.Service1.GetInfoService() in
   D:\yassine\Mobily\Log\WebService\WebService\WebService\Service1.asmx.cs:line 71

Can you please help me to find the exact error please ?

Thank you

The exception is saying exactly whats wrong, you are receiving an invalid xml that has no root element. Ask your friends to send you the raw xml by mail so you could see what they're sending you.

You can you Altova XmlSpy to verify that the xml is valid. A very basic but valid xml should be:

<root>
<child></child
</root>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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