我需要發送一個請求到Web服務。這個請求要求一個「會話」對象作爲參數。c#,不能DeSerialize XMl文件
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18060")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.xmlns.cfins.com/cfins/services/publishPolicyData/1.0")]
public partial class session {
private object[] itemsField;
private string idField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("data", typeof(sessionData))]
[System.Xml.Serialization.XmlElementAttribute("properties", typeof(sessionProperties))]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id {
get {
return this.idField;
}
set {
this.idField = value;
}
}
}
這是我的xml文件。
<session id="6055">
<properties dateModified="2014-01-08" engineVersion="2.0" cultureCode="en-US" cultureName="United States [english]">
<userName>admin</userName>
</properties>
<data id="d2311A049FDC64CD9AE9EF3DE5874CB2D">
.....
</data>
</session>
當我反序列化這個xml文件時,我只得到id,項目一直是null。順便說一下,會話類是從Web服務,我不能改變它。
public static session GetSession()
{
var s = new session();
var data = new sessionData();
var sessionXml = @"C:\Projects\CFWinSvc - Copy\XmlDeSerialize\session.xml";
using (TextReader reader = new System.IO.StreamReader(sessionXml))
{
var serializer = new XmlSerializer(typeof(session));
s = (session)serializer.Deserialize(reader);
}
return s;
}
public static session GetSession()
{
var s = new session();
var data = new sessionData();
var sessionXml = @"C:\Projects\CFWinSvc - Copy\XmlDeSerialize\session.xml";
using (TextReader reader = new System.IO.StreamReader(sessionXml))
{
var serializer = new XmlSerializer(typeof(session));
s = (session)serializer.Deserialize(reader);
}
return s;
}
UPDATE:
這裏的XML模式。
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="http://www.cfins.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.xmlns.cfins.com/cfins/services/publishPolicyData/1.0" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="session">
<xs:complexType>
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="properties">
<xs:complexType>
<xs:sequence>
<xs:element name="userName" type="xs:string"/>
</xs:sequence>
<xs:attribute name="dateModified" use="optional"/>
<xs:attribute name="engineVersion" type="xs:string" use="optional"/>
<xs:attribute name="cultureCode" type="xs:string" use="optional"/>
<xs:attribute name="cultureName" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="data">
<xs:complexType>
<xs:all>
<xs:element name="AlwaysAddIDs" type="xs:boolean" minOccurs="0"/>
<xs:element name="ProRatingOn" type="xs:boolean" minOccurs="0"/>
<xs:element name="carrier" minOccurs="0">
<xs:complexType>
<xs:attribute name="id" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="userinterface" minOccurs="0">
<xs:complexType>
<xs:attribute name="id" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
</xs:all>
<xs:attribute name="id" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
<xs:attribute name="id" use="optional"/>
</xs:complexType>
</xs:element>
</xs:schema>
我更新了我的帖子,在那裏添加了架構。謝謝你的幫助。 – qinking126
而你使用xsd.exe來編碼? –
另一位開發人員將xml加載到Visual Studio。從頂部菜單中選擇xml,然後選擇創建模式。 – qinking126