我这有一个asp.net操作xml的例子,
xml
<Question>
  <QuestionInfo>
    <title>dfasdfas</title>
    <Ftime>2008-9-8 19:27:05</Ftime>
    <QuestionDetails>dfasdfasdf</QuestionDetails>
    <EnableTime>2050-12-31 0:00:00</EnableTime>
     <Ask id="1">
      <Asktitle>dasfsdfas</Asktitle>
      <Asktype>单选题</Asktype>
      <Askcontent>dfasdfa</Askcontent> 
      <Askcontent>dasfsdfa</Askcontent>
      <Askcontent>dafsdfasd</Askcontent>
    </Ask>
  </QuestionInfo>
</Question>
我想对这个xml进行修改,ask节点下的子节点还可以增加或删除,请哪位大侠帮帮我

解决方案 »

  1.   

    xmldocument dom=new xmldocument();
    dom.loadxml(xml);
    xmlnode node=dom.selectsinglenode("//Ask[@id=1]");
    下面你可以操作了.看一上xmlnode的一些帮助即可
      

  2.   

    using System.Xml;
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("data.xml"));
            XmlNode node = doc.DocumentElement;
            XmlNode ask = node.SelectSingleNode ( "QuestionInfo/Ask" );
            XmlElement askChild = doc.CreateElement ( "name" );
            askChild.InnerText = "HelloWorld";
            ask.AppendChild ( askChild );
            doc.Save ( Server.MapPath ( "data.xml" ) );
        }
    }<Question> 
      <QuestionInfo> 
        <title>dfasdfas </title> 
        <Ftime>2008-9-8 19:27:05 </Ftime> 
        <QuestionDetails>dfasdfasdf </QuestionDetails> 
        <EnableTime>2050-12-31 0:00:00 </EnableTime> 
        <Ask id="1"> 
          <Asktitle>dasfsdfas </Asktitle> 
          <Asktype>单选题 </Asktype> 
          <Askcontent>dfasdfa </Askcontent> 
          <Askcontent>dasfsdfa </Askcontent> 
          <Askcontent>dafsdfasd </Askcontent> 
        </Ask> 
      </QuestionInfo> 
    </Question> 
      

  3.   


     /// <summary>
            /// 添加或更新数据
            /// </summary>
            public void AddOrUpdateNode(int id, string strValue)
            {
                try
                {
                    XmlNodeList studyNodes = xmlDocument.SelectNodes("Question/QuestionInfo");
                    foreach (XmlNode node in studyNodes)
                    {
                        //存在则修改
                        if (node.Attributes["id"].Value == id.ToString())
                        {
                            node.Attributes["value"].Value = strValue;
                            xmlDocument.Save(XML_PATH + XML_FILE_NAME);
                            return;
                        }
                    }                //不存在则添加
                    XmlNodeList topM = xmlDocument.ChildNodes;
                    if (topM.Count == 0)
                        return ;
                    foreach (XmlNode element in topM)
                    {
                        if (element.Name == "Ask")
                        {
                            XmlElement elem = xmlDocument.CreateElement("Asktitle");
                            XmlAttribute xaId = xmlDocument.CreateAttribute("id");
                            xaId.Value = id.ToString();
                            XmlAttribute xaValue = xmlDocument.CreateAttribute("value");
                            xaValue.Value = strValue;
                            elem.SetAttributeNode(xaId);
                            elem.SetAttributeNode(xaValue);
                            element.AppendChild(elem);
                            xmlDocument.Save(XML_PATH + XML_FILE_NAME);
                        }
                    }
                }
                catch(XmlException e)
                {
                    LogManager.Exceptions(e.Message + "\r\n" + e.Source);
                    return ;
                }
            }