How do I read and parse an XML file in C#?

后端 未结 11 2303
庸人自扰
庸人自扰 2020-11-21 23:03

How do I read and parse an XML file in C#?

11条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-21 23:23

      public void ReadXmlFile()
        {
            string path = HttpContext.Current.Server.MapPath("~/App_Data"); // Finds the location of App_Data on server.
            XmlTextReader reader = new XmlTextReader(System.IO.Path.Combine(path, "XMLFile7.xml")); //Combines the location of App_Data and the file name
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        break;
                    case XmlNodeType.Text:
                        columnNames.Add(reader.Value);
                        break;
                    case XmlNodeType.EndElement:
                        break;
                }
            }
        }
    

    You can avoid the first statement and just specify the path name in constructor of XmlTextReader.

提交回复
热议问题