将XML节点读取为c#对象

本文关键字:对象 读取 XML 节点 | 更新日期: 2023-09-27 18:13:38

我有一个xml文件。我想要读取它,之后我想要将该xml文件的值设置为我创建的对象,最后我想将这些对象添加到列表中。问题是,当我打印一些对象值时,添加列表后,所有主题都是相同的,属于最后一个<current id="3">节点。下面是xml文件和实现

XML:

<currents>
    <current id="1">
        <currentCode>1</currentCode>
        <currentName>c1</currentName>
        <currentAddress>a1</currentAddress>
        <currentTel>02321111111</currentTel>
        <fax>02321111111</fax>
        <currentProvince>İzmir</currentProvince>
        <currentCounty>Bornova</currentCounty>
        <taxOffice>Bornova1</taxOffice>
        <taxNo>111</taxNo>
        <currentType>t1</currentType>
        <postalCode>35100</postalCode>
        <countryCode>TR</countryCode>
        <additionalCurrentCode>1</additionalCurrentCode>
    </current>
    <current id="2">
        <currentCode>2</currentCode>
        <currentName>c2</currentName>
        <currentAddress>a2</currentAddress>
        <currentTel>02321111112</currentTel>
        <fax>02321111112</fax>
        <currentProvince>Aydın</currentProvince>
        <currentCounty>Nazilli</currentCounty>
        <taxOffice>Nazilli1</taxOffice>
        <taxNo>222</taxNo>
        <currentType>t2</currentType>
        <postalCode>09800</postalCode>
        <countryCode>TR</countryCode>
        <additionalCurrentCode>2</additionalCurrentCode>
    </current>
    <current id="3">
        <currentCode>3</currentCode>
        <currentName>c3</currentName>
        <currentAddress>a3</currentAddress>
        <currentTel>02321111113</currentTel>
        <fax>02321111113</fax>
        <currentProvince>İstanbul</currentProvince>
        <currentCounty>Kadıköy</currentCounty>
        <taxOffice>Kadıköy1</taxOffice>
        <taxNo>333</taxNo>
        <currentType>t3</currentType>
        <postalCode>19600</postalCode>
        <countryCode>TR</countryCode>
        <additionalCurrentCode>3</additionalCurrentCode>
    </current>
</currents>
c#:

class Program
    {
        static void Main(string[] args)
        {
            String name = "";
            String value = "";
            Current current = new Current();
            List<Current> currentList = new List<Current>();
            // Create an isntance of XmlTextReader and call Read method to read the file
            XmlTextReader textReader = new XmlTextReader("D:''c.xml");
            textReader.Read();
            // If the node has value
            while (textReader.Read())
            {
                if(textReader.NodeType == XmlNodeType.Element)
                {
                    name = textReader.Name;
                }
                if(textReader.NodeType == XmlNodeType.Text)
                {
                    switch (name)
                    {
                        case "currentCode":
                            value = textReader.Value;
                            current.CurrentCode = Convert.ToInt32(value);                               
                            break;
                        case "currentName":
                            value = textReader.Value;
                            current.CurrentName = value.ToString();
                            break;
                        case "currentAddress":
                            value = textReader.Value;
                            current.CurrentAdress = value.ToString();                                
                            break;
                        case "currentTel":
                            value = textReader.Value;
                            current.CurrentTel = value.ToString();
                            break;
                        case "fax":
                            value = textReader.Value;
                            current.Fax = value.ToString();
                            break;
                        case "currentProvince":
                            value = textReader.Value;
                            current.CurrentProvince = value.ToString();
                            break;
                        case "currentCounty":
                            value = textReader.Value;
                            current.CurrentCounty = value.ToString();
                            break;
                        case "taxOffice":
                            value = textReader.Value;
                            current.TaxOffice = value.ToString();
                            break;
                        case "taxNo":
                            value = textReader.Value;
                            current.TaxNo = value.ToString();
                            break;
                        case "currentType":
                            value = textReader.Value;
                            current.CurrentType = value.ToString();
                            break;
                        case "postalCode":
                            value = textReader.Value;
                            current.PostalCode = value.ToString();
                            break;
                        case "countryCode":
                            value = textReader.Value;
                            current.CountryCode = value.ToString();
                            break;
                        case "additionalCurrentCode":
                            value = textReader.Value;
                            current.AdditionalCurrentCode = value.ToString();
                            currentList.Add(current);
                            break;
                    } 
                }
            }
            String currentTest1 = currentList[0].CurrentProvince;
            String currentTest2 = currentList[1].CurrentProvince;
            String currentTest3 = currentList[2].CurrentProvince;
            Console.WriteLine("Test current name1:" + currentTest1);
            Console.WriteLine("Test current name2:" + currentTest2);
            Console.WriteLine("Test current name3:" + currentTest3);
            Console.ReadKey();
        }
    }

将XML节点读取为c#对象

您不必手动阅读文档。你可以使用XmlSerializer

1)复制xml文档到剪贴板(文档的内容)

2)使用VisualStudio中的PasteSpecial函数。编辑->粘贴特殊->粘贴XML AS类

或者用这些类

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class currents
{
    private currentsCurrent[] currentField;
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("current")]
    public currentsCurrent[] current
    {
        get
        {
            return this.currentField;
        }
        set
        {
            this.currentField = value;
        }
    }
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class currentsCurrent
{
    private byte currentCodeField;
    private string currentNameField;
    private string currentAddressField;
    private uint currentTelField;
    private uint faxField;
    private string currentProvinceField;
    private string currentCountyField;
    private string taxOfficeField;
    private ushort taxNoField;
    private string currentTypeField;
    private ushort postalCodeField;
    private string countryCodeField;
    private byte additionalCurrentCodeField;
    private byte idField;
    /// <remarks/>
    public byte currentCode
    {
        get
        {
            return this.currentCodeField;
        }
        set
        {
            this.currentCodeField = value;
        }
    }
    /// <remarks/>
    public string currentName
    {
        get
        {
            return this.currentNameField;
        }
        set
        {
            this.currentNameField = value;
        }
    }
    /// <remarks/>
    public string currentAddress
    {
        get
        {
            return this.currentAddressField;
        }
        set
        {
            this.currentAddressField = value;
        }
    }
    /// <remarks/>
    public uint currentTel
    {
        get
        {
            return this.currentTelField;
        }
        set
        {
            this.currentTelField = value;
        }
    }
    /// <remarks/>
    public uint fax
    {
        get
        {
            return this.faxField;
        }
        set
        {
            this.faxField = value;
        }
    }
    /// <remarks/>
    public string currentProvince
    {
        get
        {
            return this.currentProvinceField;
        }
        set
        {
            this.currentProvinceField = value;
        }
    }
    /// <remarks/>
    public string currentCounty
    {
        get
        {
            return this.currentCountyField;
        }
        set
        {
            this.currentCountyField = value;
        }
    }
    /// <remarks/>
    public string taxOffice
    {
        get
        {
            return this.taxOfficeField;
        }
        set
        {
            this.taxOfficeField = value;
        }
    }
    /// <remarks/>
    public ushort taxNo
    {
        get
        {
            return this.taxNoField;
        }
        set
        {
            this.taxNoField = value;
        }
    }
    /// <remarks/>
    public string currentType
    {
        get
        {
            return this.currentTypeField;
        }
        set
        {
            this.currentTypeField = value;
        }
    }
    /// <remarks/>
    public ushort postalCode
    {
        get
        {
            return this.postalCodeField;
        }
        set
        {
            this.postalCodeField = value;
        }
    }
    /// <remarks/>
    public string countryCode
    {
        get
        {
            return this.countryCodeField;
        }
        set
        {
            this.countryCodeField = value;
        }
    }
    /// <remarks/>
    public byte additionalCurrentCode
    {
        get
        {
            return this.additionalCurrentCodeField;
        }
        set
        {
            this.additionalCurrentCodeField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte id
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }
}

3)

        XmlSerializer serializer = new XmlSerializer(typeof(currents));
        currents stronglyTypedResult = serializer.Deserialize(new StringReader(xmlString)) as currents;
        foreach (var item in stronglyTypedResult.current)
        {
            if (item.id == 3)
                Console.WriteLine(item.countryCode);
        }