2012-09-28 2 views
2

프로그래밍에 익숙합니다. 나는 XML 양식을 사용하여 XML 파일을 만들고 XML 파일의 이름은 Windows 양식의 이름 필드 텍스트 상자 텍스트입니다.이 파일은 이미 사용 가능하지만 새로운 이름을주고 싶지만 제공 할 수 있습니다. 다른 이름은 한 번만. 예를 들어 'dog.xml'이 이미 있으면 dog1.xml 파일을 만들 수 있습니다. 새 파일을 만들 때마다 'dog1.xml'파일의 내용이 새 파일 내용으로 바뀌지 만 ' 당신이 공상하고 마이크로 소프트의 표준을 준수하려는 경우 dog11.xml '또는'dog2.xml '파일XML을 만드는 동안 XML 파일에 동적 이름을 지정하는 방법

private void btnSave_Click(object sender, EventArgs e) 
{ 
    path = rtxtName.Text + ".xml";//name of a xml file is name of WPF 'name' field 
    doc = new XmlDocument(); //Here i am creating the xmldocument object 
    doc.CreateTextNode(path); 
    if (!System.IO.File.Exists(path))//if there is no file exists then 
    { 
     CreateNewXMLDoc(); 
    } 
    else 
    { 
     path = rtxtName.Text + "1.xml"; //If the file is already avaliable   
     CreateNewXMLDoc(); 
    } 
} 

public void CreateNewXMLDoc() //This method is for creating my xml file 
{ 
    XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes"); 
    XmlComment comment = doc.CreateComment("This is a generated XML file"); 
    doc.AppendChild(declaration); 
    doc.AppendChild(comment); 
    doc.AppendChild(doc.CreateElement("root")); 
} 
+3

를 다시 시작할 때 당신이 정말로 문제가 XML과 관련이있다 생각하십니까 카운터를 계속 업데이트? –

+1

이름을 변경하는 방법에 대한 코드가 제대로 작동합니다. 문제는 어디에 있습니까? –

+2

내가 말할 수있는 한 something1.xml이 있으면 something2.xml로 저장할 수 있고, 처음 2 개가 있으면 something3.xml (실제로는 3 개, something.xml을 포함하는 경우)로 저장할 수 있기를 원합니다. – Johan

답변

5
private void btnSave_Click(object sender, EventArgs e) 
    { 
     path = rtxtName.Text;//name of a xml file is name of WPF 'name' field 

     doc = new XmlDocument(); //Here i am creating the xmldocument object 

     string tempPath = path; 
     int counter = Properties.Settings.Default.Counter; 

     while(System.IO.File.Exists(tempPath)) 
     { 
      counter++; 
      tempPath = path + counter + ".xml"; 
     } 

     Properties.Settings.Default.Counter = counter; 
     Properties.Settings.Default.Save(); 
     doc.CreateTextNode(path); 
     CreateNewXMLDoc(); 
    } 

, 당신이 뭔가를 만들 수있는이 의 경로를 변경 - Copy.xml 그 다음 무언가 - 복사 (1) .xml 등

tempPath = path + "(" + counter + ")" + ".xml"; 

EDIT

응용 프로그램이

+1

잘 작동하지만 응용 프로그램을 닫으면 다시 카운터 값이 0이됩니다. 이 문제를 해결하는 방법? –

+0

이전 카운터가 제거 될 때 어떻게됩니까? 카운터를 저장해야한다는 것을 알지 못했습니까? 즉 something1.xml이 더 이상 존재하지 않는다. 마지막 하나 (예 : something5.xml)에서 계속 진행 하시겠습니까? 아니면 something1.xml로 다시 저장 하시겠습니까? – Johan

+0

카운터를 유지할 답변을 업데이트했습니다. – Johan

관련 문제