2013-10-13 207 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文件保存路徑的方法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; 
} 

希望這有助於你保存XML文件創建和。謝謝。