2009-08-19 100 views
0

這是一個真正的新手問題,但我很努力使其發揮作用。我試圖將一些數據保存到XML文件中,但我無法弄清楚將元素放置在正確位置的語法需求。將子節點Linq保存爲XML

我希望能夠根據工廠和公司之間的關聯保存我在相應章節中構建的xml樹。代碼總是將數據添加到第一部分。我覺得我需要在_xmlEntity.Element(「Company」)。Element(「Plants」)中的where子句中添加一段時間。添加代碼的一部分,但我不明白linq足夠好,現在就可以使其工作。

隨時對您在使用此代碼時可能看到的任何其他問題發表評論,因爲我仍在學習。

這是我的(縮小)xml結構。

<?xml version="1.0" encoding="utf-8"?> 
<Entities> 
    <Company> 
    <Name>Company A</Name> 
    <Phone>(555) 516-5165</Phone> 
    <Plants> 
    <Plant> 
     <Name>Plant A</Name> 
     <EfficiencyRate>92</EfficiencyRate> 
    </Plant> 
    <Plant> 
     <Name>Plant B</Name> 
     <EfficiencyRate>92</EfficiencyRate> 
    </Plant> 
    </Plants> 
    </Company> 
    <Company> 
    <Name>Test Company</Name> 
    <Phone>(555) 123-1234</Phone> 
    <Plants /> 
    </Company> 
</Entities> 

我正在使用此代碼來構建xml並將其添加到結構中。

//Method to save plant information to XML file 
    public bool SavePlant(Plant plantData) 
    { 
     DataAcccess _newDataAccess = new DataAcccess(); 
     XElement _xmlEntity = _newDataAccess.LoadXMLFile("EntityData"); 

     //Validation code removed to make example smaller... 

     //Create xml tree for new plant entry 
     _xmlEntity.Element("Company").Element("Plants").Add(new XElement("Plant", 
      new XElement("Name", plantData.Name), 
      new XElement("Address", plantData.Address), 
      new XElement("City", plantData.City), 
      new XElement("State", plantData.State), 
      new XElement("Zip", plantData.Zip), 
      new XElement("Phone", plantData.Phone), 
      new XElement("WorkDays", plantData.WorkDays), 
      new XElement("WorkHours", plantData.WorkHours), 
      new XElement("EfficiencyRate", plantData.EfficiencyRate))); 

     //Save the tree in the EntityData.xml file and return a bool for the results 
     return _newDataAccess.SaveXMLData("EntityData", _xmlEntity); 
    } 

回答

1

那麼我能夠得到這個工作,所以我想我會張貼我使用的代碼。可悲的是,我不確定這是爲什麼會起作用,我只是盲目地蹣跚而行,直到它按照我預期的方式工作。我很樂意接受其他建議,甚至解釋爲什麼這會起作用。我不明白的是變量_xmlEntity如何使用_xmlPlantData的結果更新。我覺得我寫的代碼很愚蠢,我不明白。

public bool SavePlant(Plant plantData) 
    { 
     DataAcccess _newDataAccess = new DataAcccess(); 
     XElement _xmlEntity = _newDataAccess.LoadXMLFile("EntityData"); 

     //Validation code removed to make example easier to follow. 

     XElement _xmlPlantData = new XElement ("Plant", 
      new XElement("Name", plantData.Name), 
      new XElement("Address", plantData.Address), 
      new XElement("City", plantData.City), 
      new XElement("State", plantData.State), 
      new XElement("Zip", plantData.Zip), 
      new XElement("Phone", plantData.Phone), 
      new XElement("WorkDays", plantData.WorkDays), 
      new XElement("WorkHours", plantData.WorkHours), 
      new XElement("EfficiencyRate", plantData.EfficiencyRate)); 


     XElement _xmlTemp = _xmlEntity.Descendants("Company").First(el => (string)el.Element("Name").Value == plantData.ParentCompany); 
     _xmlTemp.Element("Plants").Add(_xmlPlantData); 

     //Save the tree in the EntityData.xml file and return a bool for the results 
     return _newDataAccess.SaveXMLData("EntityData", _xmlEntity); 
    } 

} 
+0

你不瞭解什麼? _xmlEntity包含您的XML文檔。設置_xmlTemp的代碼在該文檔中查找元素。然後添加_xmlPlantData作爲該元素的子元素。該元素仍然在您的XML文檔中,因此它會更新您的文檔。 – 2009-08-19 18:39:16

+0

我想我不明白爲什麼更新_xmlTemp會自動更新_xmlEntity。感謝您向我解釋這一點。 – 2009-08-19 19:15:16