从xml读取节点值

时间:2014-09-11 14:17:19

标签: c# xml c#-3.0

我有一个XML文件,格式为

<?xml version="1.0" encoding="utf-8"?>
<ROWDATA>
<ROW ORGCODE="00001" BRANCHCODE="00002"/>
<ROW ORGCODE="00001" BRANCHCODE="00003"/>
<ROW ORGCODE="00001" BRANCHCODE="00004"/>
<<ROW ORGCODE="00001" BRANCHCODE="00005"/>
</ROWDATA>

在读取xml时,只有第一个和第三个节点(ORGCODE =&#34; 00001&#34;,BRANCHCODE =&#34; 00002&#34;和ORGCODE =&#34; 00001&#34;,BRANCHCODE =& #34; 00004&#34;)正在读取。需要读取每个节点

我的代码结构是

 using (StreamReader oReader = new StreamReader(FileName, Encoding.GetEncoding("ISO-8859-1"))) 
     {        
            int i = 0;
            int j = 0;
                    XmlReaderSettings xrs = new XmlReaderSettings();
                    xrs.CheckCharacters = false;
                    xrs.ConformanceLevel = ConformanceLevel.Document;

                    using (XmlReader reader = XmlReader.Create(oReader, xrs))
                    {
                        while (reader.Read())
                        {
                            switch (reader.NodeType)
                            {
                                case XmlNodeType.Element:
                                    switch (reader.LocalName.ToUpper())
                                    {
                                        case "ROW":
                                            //Insert details to database
                                            if (retval == "100")
                                            {
                                                i++;
                                            }
                                            j++;
                                            obj = null;
                                            break;
                                    }
                                    break;
                            }
                        }
                    }
                }

1 个答案:

答案 0 :(得分:0)

更正XML

<?xml version="1.0" encoding="UTF-8"?>
<ROWDATA>
<ROW ORGCODE="00001" BRANCHCODE="00002"/>
<ROW ORGCODE="00001" BRANCHCODE="00003"/>
<ROW ORGCODE="00001" BRANCHCODE="00004"/>
<ROW ORGCODE="00001" BRANCHCODE="00005"/>
</ROWDATA>

波索

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class ROWDATA
{
    [System.Xml.Serialization.XmlElementAttribute("ROW")]
    public ROWDATAROW[] ROW { get; set; }
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ROWDATAROW
{
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte ORGCODE { get; set; }
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte BRANCHCODE { get; set; }
}

代码应该类似于

ROWDATA rowdata;
if (xmlstring.Deserialize(out rowdata))
{
    var element1 = rowdata.ROW[0];
    var element3 = rowdata.ROW[2];
    //do something with elements
}

要反序列化,您可以添加字符串扩展方法,如:

 public static bool Deserialize<T>(this String str, out T item)
    {
        item = default(T);
        try
        {
            using (var reader = XmlReader.Create(new StringReader(str)))
            {
                item = (T)new XmlSerializer(typeof(T)).Deserialize(reader);
            }
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }