我正在使用XML阅读器搜索我们使用的xml配置文件。我想在某个键下找到值,并且能够更改它们,或者如果它们不存在则添加。
XML示例
<DBSimulatorConfigurations>
<Configurations>
<DBSimulatorConfiguration>
<Key>Test1</Key>
<Submit>0</Submit>
<Amend>0</Amend>
<Update>0</Update>
<Delete>1</Delete>
<ResponseTimeInSeconds>100</ResponseTimeInSeconds>
</DBSimulatorConfiguration>
<DBSimulatorConfiguration>
<Key>Test2</Key>
<Submit>0</Submit>
<AutoUpdate>0</AutoUpdate>
<Amend>0</Amend>
<Update>0</Update>
<Delete>1</Delete>
<ResponseTimeInSeconds>100</ResponseTimeInSeconds>
</DBSimulatorConfiguration>
<DBSimulatorConfiguration>
</Configurations>
</DBSimulatorConfigurations>
到目前为止的代码...注释掉的位没有找到'Test1'中的值
XmlReader reader = XmlReader.Create("C:\\<path>\\DBConfigs.xml");
while(reader.Read())
{
if (reader.ReadToDescendant("Key"))
{
reader.Read();
if (reader.Value == "Test1")
{
Console.WriteLine("Found_1 {0}", reader.Value);
// Doesn't work :(
// reader.Read();
//if (reader.ReadToDescendant("Submit")) {
// Console.WriteLine("Value for Submit is {0}", reader.Value);
//}
}
if (reader.Value == "Test2")
{
Console.WriteLine("Found_2 {0}", reader.Value);
}
reader.Read(); //this moves reader to next node which is text
if (reader.ReadToDescendant("Full.2")) {
Console.WriteLine("Found {0}", reader.Value);
}
}
但我想要做的是找到并更改“提交”或“修改”等的值,或者如果没有条目“提交”,那么我将添加一个。
但首先我希望能够找到并更改Test1的值。
答案 0 :(得分:1)
这似乎可以找到价值并更新它。我已更新它,因此我可以找到特定值的任何值。
//search for all nodes with <DBSimulatorConfiguration> element
string xml = "<path>.DBConfig.xml";
XDocument xdoc = XDocument.Load(xml);
var elements = xdoc.Root.Elements().Elements().Where(x => x.Name == "DBSimulatorConfiguration");
//iterate through all those eleemnt
foreach (var element in elements)
{
//Console.WriteLine("Empty {0}", element.Value);
//now find it's child named Submit
var configKey = element.Elements().FirstOrDefault(x => x.Name == "Key");
var configSubmit = element.Elements().FirstOrDefault(x => x.Name == "Submit");
var configAmend = element.Elements().FirstOrDefault(x => x.Name == "Amend");
var configUpdate = element.Elements().FirstOrDefault(x => x.Name == "Update");
var configDelete = element.Elements().FirstOrDefault(x => x.Name == "Delete");
//if such element is found
if (configSubmit != null)
{
if (configKey.ElementsBeforeSelf().Any(x => x.Name == "Key" && x.Value == "Test1"))
{
Console.WriteLine("Found Key for Test1 {0}", configKey.Value);
}
if (configSubmit.ElementsBeforeSelf().Any(x => x.Name == "Key" && x.Value == "Test1"))
{
configSubmit.Value = "1";
Console.WriteLine("Submit value is updated to {0}", configSubmit.Value);
}
if (configAmend.ElementsBeforeSelf().Any(x => x.Name == "Key" && x.Value == "Test1"))
{
Console.WriteLine("Amend value is: {0}", configAmend.Value);
}
if (configUpdate.ElementsBeforeSelf().Any(x => x.Name == "Key" && x.Value == "Test1"))
{
Console.WriteLine("Update value is: {0}", configUpdate.Value);
}
}
}
xdoc.Save("<path>.DBConfig.xml");
这是最有效的方法吗?
答案 1 :(得分:0)
以下是一种方法,使用XDocument
:
string xml = @"
<DBSimulatorConfigurations>
<Configurations>
<DBSimulatorConfiguration>
<Key>Test1</Key>
<Submit>0</Submit>
<Amend>0</Amend>
<Update>0</Update>
<Delete>1</Delete>
<ResponseTimeInSeconds>100</ResponseTimeInSeconds>
</DBSimulatorConfiguration>
<DBSimulatorConfiguration>
<Key>Test2</Key>
<Submit>0</Submit>
<AutoUpdate>0</AutoUpdate>
<Amend>0</Amend>
<Update>0</Update>
<Delete>1</Delete>
<ResponseTimeInSeconds>100</ResponseTimeInSeconds>
</DBSimulatorConfiguration>
<DBSimulatorConfiguration>
</DBSimulatorConfiguration>
</Configurations>
</DBSimulatorConfigurations>";
XDocument xdoc = XDocument.Parse(xml);
//search for all nodes with <DBSimulatorConfiguration> element
var elements = xdoc.Root.Elements().Elements().Where(x => x.Name == "DBSimulatorConfiguration");
//iterate through all those eleemnt
foreach (var element in elements)
{
//now find it's child named Submit
var submitElement = element.Elements().FirstOrDefault(x => x.Name == "Submit");
//if such element is found
if (submitElement != null)
{
//here you can change Submit element, like this:
// submitElement.Value = "abc";
//or you can check for something
if (submitElement.ElementsBeforeSelf().Any(x=> x.Name == "Key" && x.Value== "Test2"))
{
//this is submitElement which is after element named Key with value Test2
submitElement.Value = "some specific value";
}
}
else
element.Add(new XElement("Submit", 999));
}