[英]Access class item generated from xml using xsd.exe
好吧,我使用.net xsd.exe生成了一些類來處理一些xml文件,到目前為止沒有問題。 我可以使用XmlSerializaer讀取和反類化這些類,並且可以毫無問題地讀取大多數itens。
我的問題是,當我到達像波紋管這樣的地方時:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.portalfiscal.inf.br/nfe")]
public partial class TNFeInfNFeDetImpostoPIS
{
private object itemField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("PISAliq", typeof(TNFeInfNFeDetImpostoPISPISAliq))]
[System.Xml.Serialization.XmlElementAttribute("PISNT", typeof(TNFeInfNFeDetImpostoPISPISNT))]
[System.Xml.Serialization.XmlElementAttribute("PISOutr", typeof(TNFeInfNFeDetImpostoPISPISOutr))]
[System.Xml.Serialization.XmlElementAttribute("PISQtde", typeof(TNFeInfNFeDetImpostoPISPISQtde))]
public object Item
{
get
{
return this.itemField;
}
set
{
this.itemField = value;
}
}
}
我可以訪問公共對象“ Item”,但是如何更深入? 當我到達... Pis時,我只能訪問“ Item”。
上一項的元素之一:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.portalfiscal.inf.br/nfe")]
public partial class TNFeInfNFeDetImpostoPISPISAliq
{
private TNFeInfNFeDetImpostoPISPISAliqCST cSTField;
private string vBCField;
private string pPISField;
private string vPISField;
/// <remarks/>
public TNFeInfNFeDetImpostoPISPISAliqCST CST
{
get
{
return this.cSTField;
}
set
{
this.cSTField = value;
}
}
/// <remarks/>
public string vBC
{
get
{
return this.vBCField;
}
set
{
this.vBCField = value;
}
}
/// <remarks/>
public string pPIS
{
get
{
return this.pPISField;
}
set
{
this.pPISField = value;
}
}
/// <remarks/>
public string vPIS
{
get
{
return this.vPISField;
}
set
{
this.vPISField = value;
}
}
}
我將需要訪問TNFeInfNFeDetImpostoPISPISAliq類中的公共字符串vBC,pPis和其他字符串。
這是用於生成類的XML片段(僅刪除了文檔以節省空間):
<xs:element minOccurs = "0" name="PIS">
<xs:annotation>
<xs:documentation>whatever</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:choice>
<xs:element name = "PISAliq" >
<xs:annotation>
<xs:documentation>whatever;</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name = "CST" >
<xs:annotation>
<xs:documentation>whatever;</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value = "preserve" />
<xs:enumeration value = "01" />
<xs:enumeration value = "02" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name = "vBC" type="TDec_1302">
<xs:annotation>
<xs:documentation>whatever</xs:documentation>
</xs:annotation>
</xs:element>
因此,您具有類型為object
的Item
屬性,它將被反序列化為TNFeInfNFeDetImpostoPISPISAliq
或標記為[XmlElement("PIS...", typeof(...)]
類型的其他三種類型中的任何一種。
然后,您需要在運行時確定其類型:
var deserializedObjectAliq = deserialized.Item as TNFeInfNFeDetImpostoPISPISAliq;
if (deserializedObjectAliq != null)
{
deserializedObjectAliq.WhatEver
}
var deserializedObjectNT = deserialized.Item as TNFeInfNFeDetImpostoPISPISNT;
if (deserializedObjectNT != null)
{
deserializedObjectNT.WhatEver
}
// ...
您最好為每個<xsd:choice>
生成一個成員,而不是將其存儲在一個object
,因此您只需檢查if (deserializedObject.Aliq != null) { ... }
。
我不確定是否可以告訴xsd.exe
生成這些成員。 如果沒有,則可以創建執行此操作的部分類:
public partial class TNFeInfNFeDetImpostoPIS
{
public TNFeInfNFeDetImpostoPISPISAliq Aliq
{
get
{
return Item as TNFeInfNFeDetImpostoPISPISAliq;
}
}
public TNFeInfNFeDetImpostoPISPISNT NT
{
get
{
return Item as TNFeInfNFeDetImpostoPISPISNT;
}
}
// ...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.