Home >Backend Development >C#.Net Tutorial >C# code example sharing for parsing XML files

C# code example sharing for parsing XML files

黄舟
黄舟Original
2017-03-20 13:25:212370browse

C#Sharing of code examples for parsing XML files

 XmlNodeReader reader = null;
            try
            {
                XmlDocument xd = new XmlDocument();
                xd.Load(filename);
                reader = new XmlNodeReader(xd); //创建新的XML reader
                string nodeType = null;
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    { 
                         case XmlNodeType.Element://判断读到的是否为节点
                            nodeType = reader.Name;
                            break;
                        case XmlNodeType.Text://判断读到的是否为节点值
                            switch (nodeType)
                            {
                                case ROOT:
                                    rootValue = reader.Value;
                                    break;
                                default:
                                    break;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Read XML File Error:" + e.Message + e.StackTrace);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
......................
最后不要忘记把reader close 掉

In fact, this method can be used to reduce the number of command line parameters. Number, now you only need to add a file name. But you need to write the parameters in the file

The above is the detailed content of C# code example sharing for parsing XML files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn