­

在c#中读取xml文件,用到XmlDocument类。例子如下:

 XmlDocument lXmlDocument = new XmlDocument();--------------------------------------------------新建读xml文件的对象lXmlDocument 
           lXmlDocument.Load(lFileName);--------------------------------必须有,首先要load文件名字,才可以找到文件并打开文件
            if (string.Compare(lXmlDocument.DocumentElement.Name, "BandConfig", true) == 0)
            {---------------------------------DocumentElement.Name是获取节点的标记,这里是根元素的标记是BandConfig
             ---------------------------------如果正确再读取子节点XmlNode的值                  
                foreach (XmlNode node in lXmlDocument.DocumentElement)-------注意XmlNode和DocumentElement的使用方法
                {----------------------------这里是遍历xml文件所有的子节点
                    int b = int.Parse(node.FirstChild.InnerText);---------node.FirstChild.InnerText是获取第一个元素的值。也可以是value.
                   Color c =System.Drawing.Color.FromArgb(int.Parse(node.FirstChild.NextSibling.InnerText));---子节点的兄弟节点,呵呵
                   float w = float.Parse(node.LastChild.InnerText);
                    mConfigs.Add(new BandConfigElement(b,c,w));
                   
                }
                 return true;
            }
            else
            {
                return false;
            }

posted on 2011-08-26 16:40  开心最重要  阅读(596)  评论(0编辑  收藏  举报