添加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
        }

 

相关文章:

  • 2022-12-23
  • 2021-11-19
  • 2021-09-11
  • 2021-07-18
  • 2022-01-02
猜你喜欢
  • 2022-12-23
  • 2021-10-29
  • 2021-07-11
  • 2021-05-23
  • 2022-02-10
  • 2022-03-02
相关资源
相似解决方案