C# 由于名称空间,无法反序列化xml
我想将以下xml反序列化为c#类: 问题是SessionId元素没有被反序列化,我假设是由于名称空间不匹配。我尝试使用空字符串作为名称空间,但这也不起作用,因为在反序列化过程中无法识别它C# 由于名称空间,无法反序列化xml,c#,xml,soap,C#,Xml,Soap,我想将以下xml反序列化为c#类: 问题是SessionId元素没有被反序列化,我假设是由于名称空间不匹配。我尝试使用空字符串作为名称空间,但这也不起作用,因为在反序列化过程中无法识别它 非常感谢对这个问题的任何见解。谢谢大家! 首先,SessionId的命名空间错误。它从父级继承,您需要通过将其设置为空字符串显式指定它没有父级 其次,xsi:type有一个特殊的含义,您不应该尝试反序列化它。它用于指示元素的内容类型——在本例中为string——并将由序列化程序使用。这意味着这是许多可接受的类型
非常感谢对这个问题的任何见解。谢谢大家! 首先,
SessionId
的命名空间错误。它从父级继承,您需要通过将其设置为空字符串显式指定它没有父级
其次,xsi:type
有一个特殊的含义,您不应该尝试反序列化它。它用于指示元素的内容类型——在本例中为string
——并将由序列化程序使用。这意味着这是许多可接受的类型之一。最简单的定义方法是使用对象
把这些放在一起,这应该是可行的:
[xmlement(ElementName=“SessionId”,Namespace=”“)]
公共对象SessionId{get;set;}
SessionId
应包含一个值为string
的string
对象
我还要注意其他一些小事情:
- 您不应该将所有名称空间绑定(例如
)指定为模型的一部分。这是低于此模型的级别,将由serialiser处理Xsd
- 您只需要根目录上的
XmlRoot
- 如果没有任何文本,则不需要
属性XmlText
- 您需要对上述内容进行类似的更改,以反序列化
结果
和ElementName
将默认为属性的名称,因此,如果需要,则不必指定这些名称AttributeName
公共类SessionInfo
{
[XmlElement(名称空间=”)]
公共对象SessionId{get;set;}
}
公共类标题
{
[XmlElement(命名空间=”http://www.s1.com/info/")]
public SessionInfo SessionInfo{get;set;}
}
公共类登录响应
{
[XmlElement(名称空间=”)]
公共对象结果{get;set;}
}
公共阶级团体
{
[XmlElement(命名空间=”http://www.s1.com/info/")]
公共登录响应登录响应{get;set;}
[XmlAttribute(“encodingStyle”,命名空间=”http://schemas.xmlsoap.org/soap/envelope/“,Form=XmlSchemaForm.Qualified)]
公共字符串编码样式{get;set;}
}
[XmlRoot(命名空间=”http://schemas.xmlsoap.org/soap/envelope/")]
公共类信封
{
[XmlElement]
公共标头{get;set;}
[XmlElement]
公共机构主体{get;set;}
}
您可以看到一个正在运行的演示。我对源XML做了一个更改-我将
结果
值从布尔值
更改为真值
,否则会出现异常,因为布尔值
不是该类型的有效值。添加名称空间:[xmlement(ElementName=“SessionId”,名称空间=“@jdweng我试过了,但似乎不行。将SessionId设置为字符串:[xmlement(ElementName=“SessionId”,Namespace=”“)]公共字符串SessionId{get;set;}非常感谢您不仅提供了一个有效的答案,而且还提供了解释,我还在学习,因此它对我非常有用!
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://www.s1.com/info/" xmlns:types="http://www.s1.com/info/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<tns:SessionInfo>
<SessionId xsi:type="xsd:string">string</SessionId>
</tns:SessionInfo>
</soap:Header>
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:LoginResponse>
<Result xsi:type="xsd:boolean">boolean</Result>
</tns:LoginResponse>
</soap:Body>
</soap:Envelope>
public class SessionId
{
[XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "SessionInfo", Namespace = "http://www.s1.com/info/")]
public class SessionInfo
{
[XmlElement(ElementName = "SessionId")]
public SessionId SessionId { get; set; }
[XmlAttribute(AttributeName = "id", Namespace = "")]
public string Id { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Header
{
[XmlElement(ElementName = "SessionInfo", Namespace = "http://www.s1.com/info/")]
public SessionInfo SessionInfo { get; set; }
}
[XmlRoot(ElementName = "Result", Namespace = "")]
public class Result
{
[XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "LoginResponse", Namespace = "http://www.s1.com/info/")]
public class LoginResponse
{
[XmlElement(ElementName = "Result")]
public Result Result { get; set; }
}
[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
[XmlElement(ElementName = "LoginResponse", Namespace = "http://www.s1.com/info/")]
public LoginResponse LoginResponse { get; set; }
[XmlAttribute(AttributeName = "encodingStyle", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public string EncodingStyle { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
[XmlElement(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Header Header { get; set; }
[XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsd { get; set; }
[XmlAttribute(AttributeName = "soapenc", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Soapenc { get; set; }
[XmlAttribute(AttributeName = "tns", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Tns { get; set; }
[XmlAttribute(AttributeName = "types", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Types { get; set; }
[XmlAttribute(AttributeName = "soap", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Soap { get; set; }
[XmlText]
public string Text { get; set; }
}