C# 使用xsd.exe访问从xml生成的类项

C# 使用xsd.exe访问从xml生成的类项,c#,xml,xsd,xsd.exe,C#,Xml,Xsd,Xsd.exe,我使用.net xsd.exe生成了一些类来处理一些xml文件,到目前为止没有问题。我可以使用XmlSerializaer读取和反序列化这些类,并且可以毫无问题地读取大多数iTen 我的问题是当我到达一些地方,比如下面的一个: /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")] [System.SerializableAttribute()] [System.Diag

我使用.net xsd.exe生成了一些类来处理一些xml文件,到目前为止没有问题。我可以使用XmlSerializaer读取和反序列化这些类,并且可以毫无问题地读取大多数iTen

我的问题是当我到达一些地方,比如下面的一个:

/// <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;
        }
    }
}
//
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.6.1055.0”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,命名空间=”http://www.portalfiscal.inf.br/nfe")]
公共部分类tnfeinfedetimpostopis
{
私有对象项字段;
/// 
[System.Xml.Serialization.XmlElementAttribute(“PISAliq”,typeof(tnfeinfedetimpostopisaliq))]
[System.Xml.Serialization.XmlElementAttribute(“PISNT”,typeof(tnfeinfedetimPostOposPisnt))]
[System.Xml.Serialization.xmlementAttribute(“PISOutr”,typeof(tnfeinfedetimpostopisoutr))]
[System.Xml.Serialization.XmlElementAttribute(“PISQtde”,typeof(tnfeinfedetimPostOpisPiSQTDE))]
公共对象项
{
得到
{
返回此.itemField;
}
设置
{
this.itemField=值;
}
}
}
我可以访问公共对象“Item”,但如何才能更深入? 当我到达…Pis时,我只能访问“项目”

上述项目中的一个要素:

 /// <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;
        }
    }
}
//
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.6.1055.0”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,命名空间=”http://www.portalfiscal.inf.br/nfe")]
公共部分类tnfeinfedetimpostopisaliq
{
私人Tnfeinfedetimpostopisaliqcst cSTField;
私有字符串vBCField;
私有字符串pPISField;
私有字符串vPISField;
/// 
公共技术委员会
{
得到
{
返回此.cst字段;
}
设置
{
this.cSTField=值;
}
}
/// 
公共字符串vBC
{
得到
{
返回此.vbc字段;
}
设置
{
this.vbc字段=值;
}
}
/// 
公共字符串PPI
{
得到
{
返回此.pPISField;
}
设置
{
this.pPISField=值;
}
}
/// 
公共字符串VPI
{
得到
{
返回此.vPISField;
}
设置
{
this.vPISField=值;
}
}
}
我需要访问tnfeinfedetimpostopisaliq类中的公共字符串vBC、ppi和其他字符串

下面是一段用于生成类的XML(只是删除了文档以节省空间):


无论什么
无论什么
无论什么
无论什么

因此您有一个类型为
对象的
属性
,该属性将被反序列化为
tnfeinfedetimpostopisaliq
或用
[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
}

// ...
您最好根据
生成一个成员,而不是将其存储在
对象中,这样您就可以检查
是否(反序列化对象.Aliq!=null){…}

我不确定您是否可以让
xsd.exe
生成这些成员。如果不能,您可以创建一个分部类来执行此操作:

public partial class TNFeInfNFeDetImpostoPIS
{
    public TNFeInfNFeDetImpostoPISPISAliq Aliq
    {
        get
        {
            return Item as TNFeInfNFeDetImpostoPISPISAliq;
        }
    }       

    public TNFeInfNFeDetImpostoPISPISNT NT
    {
        get
        {
            return Item as TNFeInfNFeDetImpostoPISPISNT;
        }
    }

    // ...
}

另请参见和。

因此您有一个类型为
对象的
属性,该属性将被反序列化为
tnfeinfedetimpostopisaliq
或标记有
[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
}

// ...
您最好根据
生成一个成员,而不是将其存储在
对象中,这样您就可以检查
是否(反序列化对象.Aliq!=null){…}

我不确定您是否可以让
xsd.exe
生成这些成员。如果不能,您可以创建一个分部类来执行此操作:

public partial class TNFeInfNFeDetImpostoPIS
{
    public TNFeInfNFeDetImpostoPISPISAliq Aliq
    {
        get
        {
            return Item as TNFeInfNFeDetImpostoPISPISAliq;
        }
    }       

    public TNFeInfNFeDetImpostoPISPISNT NT
    {
        get
        {
            return Item as TNFeInfNFeDetImpostoPISPISNT;
        }
    }

    // ...
}

另请参见和。

请向我们展示如何定义相应的类型是XSD。请向我们展示如何定义相应的类型是XSD.tks,对我来说也是最好的方法,只是需要为每个循环使用很多。tks,对我来说也是最好的方法,只是需要为每个循环使用很多。