C# xmlns=''> 不是预期的。- XML 文档中存在错误 (2, 2)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12672512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:03:43  来源:igfitidea点击:

xmlns=''> was not expected. - There is an error in XML document (2, 2)

c#.netxmlxml-serialization

提问by user1384603

Im trying to deserialize the response from this simple web service

我试图反序列化来自这个简单网络服务的响应

Im using the following code:

我使用以下代码:

WebRequest request = WebRequest.Create("http://inb374.jelastic.tsukaeru.net:8080/VodafoneDB/webresources/vodafone/04111111");    
WebResponse ws = request.GetResponse();
XmlSerializer s = new XmlSerializer(typeof(string));
string reponse = (string)s.Deserialize(ws.GetResponseStream());

采纳答案by L.B

Declaring XmlSerializer as

将 XmlSerializer 声明为

XmlSerializer s = new XmlSerializer(typeof(string),new XmlRootAttribute("response"));

is enough.

足够。

回答by ta.speot.is

You want to deserialize the XML and treat it as a fragment.

您想要反序列化 XML 并将其视为片段。

There's a very straightforward workaround available here. I've modified it for your scenario:

有可用的一个非常简单的解决办法在这里。我已经为你的场景修改了它:

var webRequest = WebRequest.Create("http://inb374.jelastic.tsukaeru.net:8080/VodafoneDB/webresources/vodafone/04111111");

using (var webResponse = webRequest.GetResponse())
using (var responseStream = webResponse.GetResponseStream())
{
    var rootAttribute = new XmlRootAttribute();
    rootAttribute.ElementName = "response";
    rootAttribute.IsNullable = true;

    var xmlSerializer = new XmlSerializer(typeof (string), rootAttribute);
    var response = (string) xmlSerializer.Deserialize(responseStream);
}

回答by Stefan Varga

I had the same error with Deserialize "xml string with 2 namespaces declared" into object.

我在将“声明了 2 个命名空间的 xml 字符串”反序列化为对象时遇到了同样的错误。

<?xml version="1.0" encoding="utf-8"?>
<vcs-device:errorNotification xmlns:vcs-pos="http://abc" xmlns:vcs-device="http://def">
    <errorText>Can't get PAN</errorText>
</vcs-device:errorNotification>
[XmlRoot(ElementName = "errorNotification", Namespace = "http://def")]
public class ErrorNotification
{
    [XmlAttribute(AttributeName = "vcs-pos", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string VcsPosNamespace { get; set; }

    [XmlAttribute(AttributeName = "vcs-device", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string VcsDeviceNamespace { get; set; }

    [XmlElement(ElementName = "errorText", Namespace = "")]
    public string ErrorText { get; set; }
}

By adding field with [XmlAttribute]into ErrorNotification class deserialization works.

通过将带有[XmlAttribute] 的字段添加到 ErrorNotification 类反序列化工作中。

public static T Deserialize<T>(string xml)
{
    var serializer = new XmlSerializer(typeof(T));
    using (TextReader reader = new StringReader(xml))
    {
        return (T)serializer.Deserialize(reader);
    }
}

var obj = Deserialize<ErrorNotification>(xml);