xml 모든 노드 의 개인 소결 을 읽 고 xml 노드 의 데이터 정 리 를 읽 습 니 다.

읽 기:      //파일 열기(웹.config 가 루트 디 렉 터 리 에 있다 고 가정)    string filename=Server.MapPath("/") + @"WebApplication1\web.config";     XmlDocument xmldoc= new XmlDocument();     xmldoc.Load(filename);       //최상 위 노드 목록 가 져 오기    XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;     foreach(XmlElement element in topM)      {      if(element.Name.ToLower()=="appsettings")       {         //이 노드 의 하위 노드 얻 기      XmlNodeList nodelist=element.ChildNodes;         if ( nodelist.Count >0 )        {        //DropDownList1.Items.Clear();          foreach(XmlElement el in nodelist)//읽 기 요소 값        {         //DropDownList1.Items.Add(el.Attributes["key"].InnerXml);         //this.TextBox2.Text=el.Attributes["key"].InnerText;         this.TextBox2.Text=el.Attributes["key"].Value;         this.Label1.Text=el.Attributes["value"].Value;                 //마찬가지 로 여기 서 요소 값 을 수정 할 수 있 습 니 다.뒤에서 save 할 수 있 습 니 다.          //  el.Attributes["value"].Value=this.TextBox2.Text;        }         }      }     }     xmldoc.Save(filename);   특정한 노드 에 원 소 를 추가 하고 값 을 설정 합 니 다.      if(element.Name.ToLower()=="appsettings")      {        XmlElement elem =xmldoc.CreateElement("add");            element.AppendChild(elem);      elem.InnerText="ltp";        xmldoc.Save(filename);             }   효과:            ltp      특정한 노드 에서 하나의 요 소 를 추가 하고 두 개의 속성 을 증가 합 니 다.    if(element.Name.ToLower()=="appsettings")      {        XmlElement elem =xmldoc.CreateElement("add");      element.AppendChild(elem);        XmlAttribute xa=xmldoc.CreateAttribute("key");      xa.Value="ltp";        XmlAttribute xa2=xmldoc.CreateAttribute("value");      xa2.Value="first";        elem.SetAttributeNode(xa);      elem.SetAttributeNode(xa2);        xmldoc.Save(filename);             }   효과:                  //빈 요소 추가:   XmlNode node=doc.CreateElement(groupname);      node.InnerText="";      doc.LastChild.AppendChild(node);        doc.Save(xmlfile);   노드 요소 하나 삭제   string itemname=this.listBox1.SelectedItem.ToString();        this.listBox1.Items.Remove(this.listBox1.SelectedItem);      //begin del xmlfile    XmlDocument doc=new XmlDocument();    doc.Load(xmlfile);        XmlNodeList topM=doc.DocumentElement.ChildNodes;    foreach(XmlElement element in topM)     {     if(element.Name==this.comboBox1.Text)      {        //이 노드 의 하위 노드 얻 기     XmlNodeList nodelist=element.ChildNodes;              foreach(XmlElement el in nodelist)//읽 기 요소 값      {              if(el.Attributes["key"].Value==itemname)        {        element.RemoveChild(el);       }        }//순환 원소           }//그룹 획득     }//순환 그룹   doc.Save(xmlfile);  //꼭 저장 해 야 합 니 다.그렇지 않 으 면 효과 가 없습니다  //선별 데이터private void Reader_Xml(string pathFlie) {    XmlDocument Xmldoc=new XmlDocument();    Xmldoc.Load(pathFlie);    XmlNodeList Record1=Xmldoc.DocumentElement.SelectNodes(Code[@id='1'])    int f=0;    foreach(XmlNode xnode in Record1)      {       } }
/**/*xml 데이터 읽 기   두 가지 xml 방식*/     something      something       </aa>/***//*첫 번 째 방법*/DS.ReadXml("your" xmlfile name"); Container.DataItem("bb"); Container.DataItem("cc"); DS.ReadXmlSchema("your xmlfile name");   /**/*두 번 째 방법*/    123 을 찾 아서 321 을 찾 으 려 면 어떻게 써 야 하나 요?   using System.XML; XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument(); xmlDoc.Load(@"c:\Config.xml"); XmlElement elem = xmlDoc.GetElementById("add"); string str = elem.Attributes["value"].Value     /**/*세 번 째 방법:  SelectSingleNode  두 가지 형식의 xml 읽 기 *---/ --------------------------------------------------------------------             Data Source=yf; user id=ctm_dbo;password=123                 -------------------------------------------------------------------------- XmlDocument doc = new XmlDocument(); doc.Load(strXmlName);       XmlNode node=doc.SelectSingleNode("/configuration/appSettings/ConnectionString");     if(node!=null)     {      string k1=node.Value;    //null      string k2=node.InnerText;//Data Source=yf; user id=ctm_dbo;password=123      string k3=node.InnerXml;//Data Source=yf; user id=ctm_dbo;password=123      node=null;     }   ********************************************************************                              **--------------------------------------------------------------------**      XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add");     if(node!=null)     {      string k=node.Attributes["key"].Value;      string v=node.Attributes["value"].Value;      node=null;     } *--------------------------------------------------------------------*     XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add");     if(node!=null)     {      XmlNodeReader nr=new XmlNodeReader(node);      nr.MoveToContent();     //현재 노드 가 내용 노드 인지 확인 합 니 다.이 노드 가 내용 노드 가 아니라면 리더 가 다음 내용 노드 나 파일 의 끝으로 이동 합 니 다.      nr.MoveToAttribute("value");      string s=nr.Value;      node=null;     }

좋은 웹페이지 즐겨찾기