将XML节点读取为C#对象

时间:2014-07-20 12:03:02

标签: c# xml xml-parsing

我有一个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();
        }
    }

1 个答案:

答案 0 :(得分:0)

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

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

2)使用VisualStudio中的PasteSpecial函数.Edit-&gt; Paste Special-&gt;粘贴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);
        }