简体   繁体   中英

Saving Child Nodes Linq to XML

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);
    } 

Well I was able to get this to work so I thought I would post the code that I used. Sadly I am not sure why this works, I just blindly stumbled through it until it work the way I expected. I would gladly take other suggestions or even an explanation of why this works. What I do not understand is how the variable _xmlEntity ever gets updated with the results from _xmlPlantData. I feel quite foolish having code that I wrote that I don't understand.

 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);
    } 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM