問題是這樣的,我有一個XML文件,想把它每個節點都取出來,比如一個格式化的HTML文件,想按順序遍曆一次,修改其中的內容,現以記錄的形式保存到數據庫,該怎么辦呢?

解决方案 »

  1.   

    如果能有建构的话,建议使用反系列化取出你要的各个元素
    http://blog.csdn.net/cnming/archive/2007/10/11/1819980.aspx 如果没有架构的话,就只能通过遍历了        #region XMLGetElement        public static string XMLGetElement(System.Xml.XmlNode pXN, string pElement)
            {
                try
                {
                    string pCmdName = "";                XMLGetElement(pXN, pElement, ref pCmdName);                return pCmdName;
                }
                catch (NullReferenceException NullEx)
                {
                    throw NullEx;
                }
                catch (Exception Ex)
                {
                    throw Ex;
                }
            }        #endregion XMLGetElement        public static void XMLGetElement(System.Xml.XmlNode pXN, string pElement, ref string pCmdName)
            {
                try
                {
                    if (pXN.Name == pElement)
                    {
                        pCmdName = pXN.InnerText;
                        return;
                    }                foreach (System.Xml.XmlNode mXN in pXN)
                    {
                        if (mXN.ChildNodes.Count > 0)
                        {
                            XMLGetElement(mXN, pElement, ref pCmdName);
                        }
                    }
                }
                catch (NullReferenceException NullEx)
                {
                    throw NullEx;
                }
                catch (Exception Ex)
                {
                    throw Ex;
                }
            }
      

  2.   

    如果文件比较大,使用XmlReader
    否则XmlDocument也可以具体语法参照MSDN~~~~~
      

  3.   

    读取放入一个listbox
    public void ReadXmlAttributes()
      {
       this._listBox.Items.Clear();
       this.xmlTxtRd = new XmlTextReader(this._xmlPath);   try
       {
        while(xmlTxtRd.Read())
        {
         if (xmlTxtRd.NodeType == XmlNodeType.Element)
         {
          if (xmlTxtRd.HasAttributes)
          {
           this._listBox.Items.Add("The Element " + xmlTxtRd.Name + " has " + xmlTxtRd.AttributeCount + " Attributes");       this._listBox.Items.Add("The Attributes are:");       while(xmlTxtRd.MoveToNextAttribute())
           {
            this._listBox.Items.Add(xmlTxtRd.Name + " = " + xmlTxtRd.Value);
           }
          }
          else
          {
           this._listBox.Items.Add("The Element " + xmlTxtRd.Name + " has no Attribute");
          }
          this._listBox.Items.Add("");
         }
        }
       }
       catch(XmlException xmlExp)
       {
        throw new XmlException(_errMsg + this._xmlPath + xmlExp.ToString());
       }
       finally
       {
        if (this.xmlTxtRd != null)
         this.xmlTxtRd.Close();
       }
      }