简体   繁体   English

通过C#Web服务接收XML的确切方法

[英]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 : .... 我已经创建了一个Web服务来接收XML文件,因此我遵循以下方法,然后发布了它,并且对我来说效果很好:....

 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: 知道我的XML结构是:

<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; 但是在经过一些朋友的测试之后,从Lunix平台调用了我的Web服务,我在日志文件错误中收到以下消息错误; knowing that their XML file is validated. 知道他们的XML文件已经过验证。

Just to let you know; 只是让你知道; that their XML file did not contains the declaration of: 他们的XML文件不包含以下声明:

<?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. 例外是确切说明出了什么问题,您收到的无效的xml没有根元素。 Ask your friends to send you the raw xml by mail so you could see what they're sending you. 要求您的朋友通过邮件向您发送原始xml,以便您可以看到他们向您发送的内容。

You can you Altova XmlSpy to verify that the xml is valid. 您可以Altova XmlSpy来验证xml是否有效。 A very basic but valid xml should be: 一个非常基本但有效的xml应该是:

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

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

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