XML解析

来源:互联网 发布:ubuntu 所有用户命令 编辑:程序博客网 时间:2024/05/22 13:54

文本内容:

<?xml version="1.0" encoding="utf-8"?>
<dataList xmlns="DataNameSpace">
  <boolSection>
    <bool name="bCycle" public="false" privilege="0">
      <valueBool index="0" value="true" />
    </bool>
  </boolSection>
</dataList>

 

函数

        public  bool ParseR1(byte[] l_data)
        {
      

            try
            {

                if (l_data == null)
                {
                    return false;
                }
                string dataindex = null;
                string[] data = new string[200];
                int id = 0;
                string dataname = null;
                MemoryStream l_stream = new MemoryStream(l_data);
                l_stream.Position = 0;
                StreamReader l_read = new StreamReader(l_stream);
                XmlDocument l_doc = new XmlDocument();
                l_doc.LoadXml(l_read.ReadToEnd());
                XmlNamespaceManager l_nsMgr = new XmlNamespaceManager(l_doc.NameTable);
                l_nsMgr.AddNamespace("dtx", l_doc.DocumentElement.NamespaceURI);
                XmlNode l_valueNode;
                XmlAttribute l_val;
                string l_search = null;
                XmlElement root;
                XmlNodeList nodelist;

                //*****************************************************************************************
                dataindex = "value";
                dataname = "bCycle";
                id = 0;
                l_search = @"/dtx:dataList/dtx:boolSection/dtx:bool[@name='" + dataname + @"']/dtx:valueBool[@index='" + id.ToString() + "']";
                l_valueNode = l_doc.DocumentElement.SelectSingleNode(l_search, l_nsMgr);
                if (l_valueNode!= null)
                {
                    l_val = l_valueNode.Attributes[dataindex];
                    data[0] = (l_val != null) ? l_val.Value.ToString() : null;
                }
                else
                {
                    data[0] = null;
                }

                //*****************************************************************************************

                //
                if (data[0] != null && data[0] == "true")
                {
                    GlobleData.R1var.Cycle = true;

                   
                }

                else
                {
                    GlobleData.R1var.Cycle= false;

                }
               
                return true;
            }

            catch
            {
                return false;
            }

        }

 

原创粉丝点击