locked
wp7下如何解析XML格式的string? RRS feed

  • 问题

  • wp7下无法使用XmlDocument类,那么如何去解析XML格式的string,并读取里面节点的内容?求实例代码。
    2012年7月24日 9:36

答案

  • 用XmlReader(using System.Xml),下面是我的一段代码

           public bool openXmlFile()
            {
                XmlReader reader = XmlReader.Create("Data/desc.xml");
                if (null == reader)
                    return false;

                while (reader.Read())
                {
                    if (XmlNodeType.Element == reader.NodeType)
                    {
                        if( "title" == reader.LocalName)
                        {
                            reader.Read();
                            if( XmlNodeType.CDATA == reader.NodeType)
                            {
                                m_title = reader.ReadContentAsString();
                            }
                        }
                        else if ("author" == reader.LocalName)
                        {
                            reader.Read();
                            if (XmlNodeType.CDATA == reader.NodeType)
                            {
                                m_author = reader.ReadContentAsString();
                            }
                        }
                        else if ("category" == reader.LocalName)
                        {
                            reader.Read();
                            if (XmlNodeType.CDATA == reader.NodeType)
                            {
                                m_type = reader.ReadContentAsString();
                            }
                        }
                        else if ("description" == reader.LocalName)
                        {
                            reader.Read();
                            if (XmlNodeType.CDATA == reader.NodeType)
                            {
                                m_description = reader.ReadContentAsString();
                            }
                        }
                    }
               }

                return true;
            }

    2012年7月25日 2:22

全部回复

  • 用XmlReader(using System.Xml),下面是我的一段代码

           public bool openXmlFile()
            {
                XmlReader reader = XmlReader.Create("Data/desc.xml");
                if (null == reader)
                    return false;

                while (reader.Read())
                {
                    if (XmlNodeType.Element == reader.NodeType)
                    {
                        if( "title" == reader.LocalName)
                        {
                            reader.Read();
                            if( XmlNodeType.CDATA == reader.NodeType)
                            {
                                m_title = reader.ReadContentAsString();
                            }
                        }
                        else if ("author" == reader.LocalName)
                        {
                            reader.Read();
                            if (XmlNodeType.CDATA == reader.NodeType)
                            {
                                m_author = reader.ReadContentAsString();
                            }
                        }
                        else if ("category" == reader.LocalName)
                        {
                            reader.Read();
                            if (XmlNodeType.CDATA == reader.NodeType)
                            {
                                m_type = reader.ReadContentAsString();
                            }
                        }
                        else if ("description" == reader.LocalName)
                        {
                            reader.Read();
                            if (XmlNodeType.CDATA == reader.NodeType)
                            {
                                m_description = reader.ReadContentAsString();
                            }
                        }
                    }
               }

                return true;
            }

    2012年7月25日 2:22
  • Xml文件片段如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <book>
     <title><![CDATA[书名]]></title>
     <author><![CDATA[作者]]></author>
     <description><![CDATA[简介]]></description>
     <category><![CDATA[分类]]></category>
     </book>

    2012年7月25日 2:27