2012-02-08 2 views
7

"XMLWriter"를 사용할 때마다 새로운 XML 파일을 생성 할 때마다 xmldoc을 사용하여 XML 파일을로드 한 다음 여기에 코드를 추가하는 것이 좋습니다. 그러나 " 이 문서에는 이미 'DocumentElement'노드가 있습니다. " 여기 xmlwriter를 사용하여 xml 파일을 추가합니다.

//Append to xml file 


      XmlDocument doc = new XmlDocument(); 
      doc.Load(@"c:\\test.xml"); 
      using (XmlWriter xmlWrite = doc.CreateNavigator().AppendChild()) 
      { 
       xmlWrite.WriteStartElement("image name=",Name); 
       xmlWrite.WriteElementString("width", widthValue[1]); 
       xmlWrite.WriteElementString("Height", heightValue[1]); 
       xmlWrite.WriteElementString("file-size", FileSizeValue[1]); 
       xmlWrite.WriteElementString("file-format", FileFormatValue[1]); 
       xmlWrite.WriteElementString("resolution", ResolutionValue[1]); 
       xmlWrite.Close(); 
      } 

내 샘플 test.xml의입니다

<job-metadata> 
    <slug>730s_Sales/CupWinner_0111</slug> 
    <locations>Africa</locations> 
    <primary-location>Africa</primary-location> 
    <reporter>Leigh Sales</reporter> 
    <genre>Current</genre> 
    <copyright>CBS</copyright> 
    <autopublish>true</autopublish> 
</job-metadata> 

하고있는 XML 데이터를 재생하려면 사전

<job-metadata> 
    <slug>730s_Sales/CupWinner_0111</slug> 
    <locations>Africa</locations> 
    <primary-location>Africa</primary-location> 
    <reporter>Leigh Sales</reporter> 
    <genre>Current</genre> 
    <copyright>CBS</copyright> 
    <autopublish>true</autopublish> 
- <image name="557684_20111101-730s_SalesCupWinner_0111_80x60.jpg"> 
     <width>80</width> 
     <height>60</height> 
     <file-size>7045</file-size> 
     <file-format>JPEG Baseline</file-format> 
     <resolution>72</resolution> 
     <custom-name>newsthumbnail</custom-name> 
    </image> 
</job-metadata> 

아래 덕분에 같은 XML에

+0

이 도움이됩니다 당신은 result'll 그런 일이

XmlDocument xmlDoc=new XmlDocument(); xmlDoc.Load("F:/Documents and Settings/Administrator/Desktop/Account.xml"); XmlElement subRoot=xmlDoc.CreateElement("User"); //UserName XmlElement appendedElementUsername=xmlDoc.CreateElement("UserName"); XmlText xmlTextUserName=xmlDoc.CreateTextNode(txtUsrName.Text.Trim()); appendedElementUsername.AppendChild(xmlTextUserName); subRoot.AppendChild(appendedElementUsername); xmlDoc.DocumentElement.AppendChild(subRoot); //Email XmlElement appendedElementEmail=xmlDoc.CreateElement("Email"); XmlText xmlTextEmail=xmlDoc.CreateTextNode(txtEmail.Text.Trim()); appendedElementEmail.AppendChild(xmlTextEmail); subRoot.AppendChild(appendedElementEmail); xmlDoc.DocumentElement.AppendChild(subRoot); xmlDoc.Save("F:/Documents and Settings/Administrator/Desktop/Account.xml");if(!File.Exists("F:/Documents and Settings/Administrator/Desktop/Account.xml")) { XmlTextWriter textWritter=new XmlTextWriter("F:/Documents and Settings/Administrator/Desktop/Account.xml", null); textWritter.WriteStartDocument(); textWritter.WriteStartElement("USERS"); textWritter.WriteEndElement(); textWritter.Close(); } XmlDocument xmlDoc=new XmlDocument(); xmlDoc.Load("F:/Documents and Settings/Administrator/Desktop/Account.xml"); XmlElement subRoot=xmlDoc.CreateElement("User"); //UserName XmlElement appendedElementUsername=xmlDoc.CreateElement("UserName"); XmlText xmlTextUserName=xmlDoc.CreateTextNode(txtUsrName.Text.Trim()); appendedElementUsername.AppendChild(xmlTextUserName); subRoot.AppendChild(appendedElementUsername); xmlDoc.DocumentElement.AppendChild(subRoot); //Email XmlElement appendedElementEmail=xmlDoc.CreateElement("Email"); XmlText xmlTextEmail=xmlDoc.CreateTextNode(txtEmail.Text.Trim()); appendedElementEmail.AppendChild(xmlTextEmail); subRoot.AppendChild(appendedElementEmail); xmlDoc.DocumentElement.AppendChild(subRoot); xmlDoc.Save("F:/Documents and Settings/Administrator/Desktop/Account.xml"); 

같은 것을 할 필요가이

//add to elements collection doc.DocumentElement.AppendChild(node); 

같은 uemnt? ................. –

+0

절대적으로 고마워요. 예를 들어, 아직 수정하지 않았습니다. 별표를 표시합니다. 지금 글 쓰는 중. 여기에 코드를 다시 게시합니다. 다시 한 번 감사드립니다. – Usher

답변

4

를 추가하려고 .net 버전 3.5를 사용하는 경우 더 나은 사용자 LINQ to XML입니다.

http://www.codeproject.com/Articles/24376/LINQ-to-XML

또는

Manipulate XML data with XPath and XmlDocument (C#)

또는

기사 : How to Append to a Large XML File

나는 당신이 당신의 해당 xmldoc에 노드를 추가 할 필요가 thnik

</USERS> 
<User> 
<UserName>Buggaya</UserName> 

<Email>[email protected]</Email> 
</User> 
</USERS> 

는 오리지널 포스트 : Append in xml document

관련 문제