0

I have this object here:

public class ServiceConfig
{
    public List<DatabaseDescriptor> Databases { get; set; }  
}

public class DatabaseDescriptor
{
    [XmlElement("Name")]
    public string Name { get; set; }
    public List<Table> Tables { get; set; }        
}

public class Table
{  
    [XmlElement("ID")]
    public string ID { get; set; }
    [XmlElement("TableName")]
    public string TableName { get; set; }
}

I have

public xml.ServiceConfig xmlData { get; set; }

How do i update the xmlData DatabaseDescriptors where the name == test i.e How do i update it?

SERIALIZING BACK TO XML

XmlSerializer serializer = new XmlSerializer(typeof(xml.ServiceConfig));
            using (TextWriter writer = new StreamWriter(@"C:\Xml.xml"))
            {
                serializer.Serialize(writer, ServiceConfig);
            } 
Mal
  • 17
  • 4

2 Answers2

0

Try:

DatabaseDescriptor updateObject = xmlData.Databases.FirstOrDefault(_d => _d.Name.Equals("Test"));
if(updateObject != null)
{
    // update what you want to update...
}
blfuentes
  • 2,731
  • 5
  • 44
  • 72
0

if name is unique

using System.Linq;


DatabaseDescriptor db = xmlData.Databases.SingleOrDefault(db => db.name == "test");
if (db != null)
{
                //Update db instance
}

else

    var dbs = xmlData.Databases.Where(db => db.name == "test");

    foreach (DatabaseDescriptor element in dbs)
    {
        // Update elements
    }

if you want to serialize/deserialize in xml Databases property you must decorate with [XmlArray],

you see Deserialize XML does not populate array

Community
  • 1
  • 1
Fabio
  • 1,890
  • 1
  • 15
  • 19
  • Thanks, this looks good :) Please can you tell me how to serialize my ServiceConfig back to a xml file?, i'm using the xml writer but maybe my cod eis not correct, please see my edit – Mal Oct 24 '14 at 07:30
  • What's not correct? List doesn't come serialized? – Fabio Oct 24 '14 at 07:58