我这里有这个对象:
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; }
}
我有
public xml.ServiceConfig xmlData { get; set; }
如何更新名称==测试的xmlData DatabaseDescriptors,即 我如何更新它?
SERIALIZING BACK TO XML
XmlSerializer serializer = new XmlSerializer(typeof(xml.ServiceConfig));
using (TextWriter writer = new StreamWriter(@"C:\Xml.xml"))
{
serializer.Serialize(writer, ServiceConfig);
}
答案 0 :(得分:0)
尝试:
DatabaseDescriptor updateObject = xmlData.Databases.FirstOrDefault(_d => _d.Name.Equals("Test"));
if(updateObject != null)
{
// update what you want to update...
}
答案 1 :(得分:0)
如果名称是唯一的
using System.Linq;
DatabaseDescriptor db = xmlData.Databases.SingleOrDefault(db => db.name == "test");
if (db != null)
{
//Update db instance
}
否则
var dbs = xmlData.Databases.Where(db => db.name == "test");
foreach (DatabaseDescriptor element in dbs)
{
// Update elements
}
如果要在xml Databases属性中序列化/反序列化,则必须使用[XmlArray]进行装饰,