首页 > 代码库 > [c# 20问] 2.如何转换XML文件

[c# 20问] 2.如何转换XML文件

添加System.Xml引用

使用XmlReader转换字符串

DEMO
        #region Parse Xml        private static void ParseXml(string xmlString)        {            StringBuilder output = new StringBuilder();            using(XmlReader reader= XmlReader.Create(new StringReader(xmlString)))            {                reader.ReadToFollowing("book");                reader.MoveToFirstAttribute();                output.AppendLine("The genre value:"+reader.Value);                reader.ReadToFollowing("title");                output.AppendLine("Conten of the title element:"+reader.ReadElementContentAsString());            }            Console.WriteLine(output);        }        #endregion        static void Main(string[] args)        {            #region Parse Xml            String xmlString =                @"<bookstore>                     <book genre=‘autobiography‘ pubicationdate=‘1981-3-22‘ ISBN=‘1-861003-11-0‘>                         <title>The Autobiograph of Benamin Franklin</title>                         <author>                             <first-name>Benjamin</first-name>                             <last-name>Franklin</last-name>                         </author>                         <price>8.99</price>                     </book>                  </bookstore>";            ParseXml(xmlString);            Console.ReadLine();            #endregion        }