在 C# 中使用 XmlReader 在 xml 段中查找值
Finding a value within an xml segment using XmlReader in C#
我正在使用 XML reader 来搜索我们使用的 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);
}
}
但我想做的是找到并更改 'Submit' 或 'Amend' 等的值,或者如果没有条目 'Submit' 那么我将添加一个。
但首先我希望能够找到并更改 Test1 的值。
这是一种方法,使用 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));
}
这似乎可以找到价值并更新它。我已经更新了它,所以我可以找到特定值的任何值。
//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");
这是最有效的方法吗?
我正在使用 XML reader 来搜索我们使用的 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);
}
}
但我想做的是找到并更改 'Submit' 或 'Amend' 等的值,或者如果没有条目 'Submit' 那么我将添加一个。
但首先我希望能够找到并更改 Test1 的值。
这是一种方法,使用 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));
}
这似乎可以找到价值并更新它。我已经更新了它,所以我可以找到特定值的任何值。
//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");
这是最有效的方法吗?