我是c#的新手,我正在努力研究如何从一个已经序列化为类的xml中获取值。
我使用xsd构建了类,如我在堆栈中找到的线程中所述。
任何人都可以帮助我获得LoginState的价值吗?
这是我在调试时可以看到的......
这是我将其序列化为的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace vCreate.Model
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class NewDataSet
{
private object[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Table", typeof(NewDataSetTable), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("Table1", typeof(NewDataSetTable1), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public object[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class NewDataSetTable
{
private string loginStateField;
private string loginMessageField;
private string authIDField;
private string userIDField;
private string companyIDField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string LoginState
{
get
{
return this.loginStateField;
}
set
{
this.loginStateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string LoginMessage
{
get
{
return this.loginMessageField;
}
set
{
this.loginMessageField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string AuthID
{
get
{
return this.authIDField;
}
set
{
this.authIDField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string UserID
{
get
{
return this.userIDField;
}
set
{
this.userIDField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string CompanyID
{
get
{
return this.companyIDField;
}
set
{
this.companyIDField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class NewDataSetTable1
{
private string languageIDField;
private string languageDescriptionField;
private string langLargeImageField;
private string langThumbImageField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string LanguageID
{
get
{
return this.languageIDField;
}
set
{
this.languageIDField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string LanguageDescription
{
get
{
return this.languageDescriptionField;
}
set
{
this.languageDescriptionField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string LangLargeImage
{
get
{
return this.langLargeImageField;
}
set
{
this.langLargeImageField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string LangThumbImage
{
get
{
return this.langThumbImageField;
}
set
{
this.langThumbImageField = value;
}
}
}
}
答案 0 :(得分:2)
因为我们都明白你需要做的是resultingMessage.Items[0].LoginState
来获取登录状态并将其放在一个整数中。
int state = (resultingMessage.Items[0] as NewDataSetTable).LoginState;
答案 1 :(得分:1)
现在您已经对XML进行了反序列化,您所要做的就是从数据集中获取它。试试以下
foreach (DataTable table in resultingMessage.Tables)
{
foreach (DataRow row in table.Rows)
{
foreach (object item in row.ItemArray)
{
// read item
var loginState = item.LoginState;
}
}
}