This is a real newbie question, but I am struggling to get this to work. I am trying to save some data into a XML file, but I can't figure out the syntax need to place the elements in the correct location.
I would like to be able to save the xml tree that I built in the corresponding sections based on an association between the plant and the company. The code as is always adds the data to the first section. I feel like I need some time of where clause in the _xmlEntity.Element("Company").Element("Plants").Add section of the code, but I do not understand linq well enough right now to get it to work.
Feel free to comment on any other issues you may see with this code as I am still learning.
This is my (scaled down) xml structure.
<?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>
I am using this code to build the xml and adding it to the structure.
//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);
}