正在从xml中读取节点值

本文关键字:读取 节点 xml | 更新日期: 2023-09-27 18:21:28

我有一个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="00001",BRANCHCODE="000002"和ORGCODE="00002",BRANCHCODE="00004")正在获取。需要读取每个节点

我的代码结构是

 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;
                            }
                        }
                    }
                }

正在从xml中读取节点值

更正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>

Poco

[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;
        }
    }