2013-10-13 2 views
0

이 XML 파일은이 형식으로되어 있습니다.XML 파일 만들기

<Questions> 
    <Question> 
     <questiontext>The remains of the Tabon man was discovered in the Tabon Caves in  </questiontext> 
     <choice1>Lipuun Point</choice1> 
     <choice2>Callao Cave</choice2> 
     <choice3>Hinagdanan Cave</choice3> 
     <choice4>Montfort Bat Sanctuary</choice4> 
     <answer>Lipuun Point</answer> 
    </Question> 
</Questions> 

나는 메모장을 통해 이것을 만들고 있습니다. ++. 그리고 이것을 이렇게 읽으십시오.

System.IO.Stream stream = TitleContainer.OpenStream("Content//Level1Trivia.xml"); 
XDocument doc = XDocument.Load(stream);    
level1Trivia = new List<Trivias>(); 
level1Trivia = (from question in doc.Descendants("Question") 
    select new Trivias() 
    { 
     Question = question.Element("questiontext").Value, 
     Choice1 = question.Element("choice1").Value, 
     Choice2 = question.Element("choice2").Value, 
     Choice3 = question.Element("choice3").Value, 
     Choice4 = question.Element("choice4").Value, 
     Answer = question.Element("answer").Value, 
    }).ToList(); 

문제가 있습니다. 외부에서 작성된 XML 파일을 읽을 수 있습니다. 하지만 코드를 통해 XML 파일을 작성/작성하는 방법을 모르겠습니다. 그리고 주어진 코드를 사용하여 읽으십시오. 어떤 아이디어? 감사!

답변

1

XElement 개체를 사용하십시오. 예 :

XDocument document = new XDocument(); 
XElement rootElement = new XElement("Questions"); 

foreach(Question question in QuestionsCollection) 
{ 
    XElement questionElement = new XElement("Question"); 

    questionElement.Add(new XElement("questiontext") { Value = question.Text }); 
    questionElement.Add(new XElement("choice1") { Value = question.Question1 }); 
    questionElement.Add(new XElement("choice2") { Value = question.Question2 }); 
    questionElement.Add(new XElement("choice3") { Value = question.Question3 }); 
    questionElement.Add(new XElement("choice4") { Value = question.Question4 }); 
    questionElement.Add(new XElement("answer") { Value = question.Answer }); 

    rootElement.Add(questionElement); 
} 

document.Add(rootElement); 
document.Save("C:\Location.xml"); 

그런 식으로 작동해야합니다.

+0

나는 이것을 시도 할 것이다. 콘텐츠 폴더에 어떻게 저장할 수 있습니까? – ljpv14

+0

콘텐츠 란 프로그램이있는 폴더와 동일한 폴더를 의미한다고 가정합니다. 몇 가지 방법이 있습니다. 필자가 선호하는 방법은 단지 "document.Save ("File.xml ");"로, 드라이브를 지정하지 않으면 경로가 프로그램의 실행 경로이기 때문입니다. – Falgantil

+0

파일이 있는지 확인하는 방법이 있습니까? XML에 데이터를 저장하려고하기 때문에. 파일이 이미있는 경우. 데이터를 가져 와서 한 세트의 데이터를 추가하고 싶습니다. – ljpv14

0

사용이 코드를 작성하고 저장 XML 파일은 XML이 방법 CreateXml이 링크이 당신을 도와줍니다 Create and Save XML file in C#/VB

public void CreateXml(string XmlPath) 
{ 
try 
      { 
       if (ds.Tables[0].Rows.Count > 0) 
       { 
        string Name = string.Empty; 
        int Age = 0; 
        int Experience = 0; 

        Name = ds.Tables[0].Rows[0]["EmployeeName"].ToString(); 
        Age = int.Parse(ds.Tables[0].Rows[0]["EmployeeAge"].ToString()); 
        Experience = int.Parse(ds.Tables[0].Rows[0]["EmployeeExperience"].ToString()); 

        string xml = XmlTemplate().ToString().Replace("EmpName", Name).Replace("EmpAge", Age.ToString(),Replace("EmpExperience", Experience.ToString()); 
        XmlPath = XmlPath + "Employee_" + Name + ".xml"; 
        XmlDocument xdoc = new XmlDocument(); 
        xdoc.LoadXml(xml); 
        xdoc.Save(XmlPath); 
        lblMessage.Text = "XML Created Successfully."; 
       } 
       else 
       { 
        lblMessage.Text = "InValid Employee ID."; 
       } 
      } 
      catch (Exception ex) 
      { 
       lblMessage.Text = ex.Message.ToString(); 
      } 
} 


public string XmlTemplate() 
{ 

      string Xml = "<Employee>" + 
          "<Name>EmpName</Name>" + 
          "<Age>EmpAge</Age>" + 
       "<Experience>EmpExperience</Experience>" + 
          "</Employee>"; 
      return Xml; 
} 

희망

체크 아웃에 파일 경로를 저장 전달하여합니다. 감사.