C# 如何读取和更新XElement中节点的值
我将一个xml文档(实际上是一个配置文件)加载到XDocument对象中,该对象包含如下元素:C# 如何读取和更新XElement中节点的值,c#,xml,linq,C#,Xml,Linq,我将一个xml文档(实际上是一个配置文件)加载到XDocument对象中,该对象包含如下元素: <ScheduledTasks> <add key="RelativePath" value="..\Scheduler\Tasks"/> <add key="SearchPauseInSeconds" value="10"/> <add key="MatrixAccount" value="95755UE93ZEb3fRZUSZ7
<ScheduledTasks>
<add key="RelativePath" value="..\Scheduler\Tasks"/>
<add key="SearchPauseInSeconds" value="10"/>
<add key="MatrixAccount" value="95755UE93ZEb3fRZUSZ753K9FRS3O9DaDrJxtdiiZnm"/>
<add key="MatrixPassword" value="95755UE93ZEb3fRZUSZ753K9FRS3O9DaDgKrn2e71"/>
</ScheduledTasks>
如何最好地检索(和更新)RelativePath、SeachPauseInseconds等的值?它们不是元素
TIA。它们是属性。使用
XElement.Attribute(“attributeName”)
获取它们
var items = (from i in scheduledTasksElement.Elements("add")
select new
{
KeyAttribute = i.Attribute("key"),
Key = (string)i.Attribute("key"),
ValueAttribute = i.Attribute("value"),
Value = (string)i.Attribute("value")
}).ToList();
如您所见,您可以轻松地将XAttribute
转换为其他类型,就像使用XElement
一样
您还可以更新该值:
items[0].KeyAttribute.Value = "newValue";
它们是属性。使用
XElement.Attribute(“attributeName”)
获取它们
var items = (from i in scheduledTasksElement.Elements("add")
select new
{
KeyAttribute = i.Attribute("key"),
Key = (string)i.Attribute("key"),
ValueAttribute = i.Attribute("value"),
Value = (string)i.Attribute("value")
}).ToList();
如您所见,您可以轻松地将XAttribute
转换为其他类型,就像使用XElement
一样
您还可以更新该值:
items[0].KeyAttribute.Value = "newValue";
它们是属性。使用
XElement.Attribute(“attributeName”)
获取它们
var items = (from i in scheduledTasksElement.Elements("add")
select new
{
KeyAttribute = i.Attribute("key"),
Key = (string)i.Attribute("key"),
ValueAttribute = i.Attribute("value"),
Value = (string)i.Attribute("value")
}).ToList();
如您所见,您可以轻松地将XAttribute
转换为其他类型,就像使用XElement
一样
您还可以更新该值:
items[0].KeyAttribute.Value = "newValue";
它们是属性。使用
XElement.Attribute(“attributeName”)
获取它们
var items = (from i in scheduledTasksElement.Elements("add")
select new
{
KeyAttribute = i.Attribute("key"),
Key = (string)i.Attribute("key"),
ValueAttribute = i.Attribute("value"),
Value = (string)i.Attribute("value")
}).ToList();
如您所见,您可以轻松地将XAttribute
转换为其他类型,就像使用XElement
一样
您还可以更新该值:
items[0].KeyAttribute.Value = "newValue";
例如,您可以创建一个扩展方法来实现它
public static void FindAndReplace(this XDocument doc, string key, string newValue)
{
var elem = doc.Descendants("add")
.FirstOrDefault(d => d.Attribute("key").Value == key);
if (elem != null)
elem.Attribute("value").Value = newValue;
}
像这样使用它
doc.FindAndReplace("RelativePath", "..\Tasks");
例如,您可以创建一个扩展方法来实现它
public static void FindAndReplace(this XDocument doc, string key, string newValue)
{
var elem = doc.Descendants("add")
.FirstOrDefault(d => d.Attribute("key").Value == key);
if (elem != null)
elem.Attribute("value").Value = newValue;
}
像这样使用它
doc.FindAndReplace("RelativePath", "..\Tasks");
例如,您可以创建一个扩展方法来实现它
public static void FindAndReplace(this XDocument doc, string key, string newValue)
{
var elem = doc.Descendants("add")
.FirstOrDefault(d => d.Attribute("key").Value == key);
if (elem != null)
elem.Attribute("value").Value = newValue;
}
像这样使用它
doc.FindAndReplace("RelativePath", "..\Tasks");
例如,您可以创建一个扩展方法来实现它
public static void FindAndReplace(this XDocument doc, string key, string newValue)
{
var elem = doc.Descendants("add")
.FirstOrDefault(d => d.Attribute("key").Value == key);
if (elem != null)
elem.Attribute("value").Value = newValue;
}
像这样使用它
doc.FindAndReplace("RelativePath", "..\Tasks");
所涉及的
XElement
s是add
s,带有XAttribute
skey
和value
。我明白了-XElement“ScheduledTasks”包含几个XElement“add”,每个XElement都有属性“key”和“value”。是这样吗?因此,我需要在每种情况下获得“value”属性的值?所涉及的XElement
s是add
s,带有XAttribute
skey
和value
。我得到了它-XElement“scheduledtask”包含多个元素“add”,每个元素都带有属性“key”和“value”。是这样吗?因此,我需要在每种情况下获得“value”属性的值?所涉及的XElement
s是add
s,带有XAttribute
skey
和value
。我得到了它-XElement“scheduledtask”包含多个元素“add”,每个元素都带有属性“key”和“value”。是这样吗?因此,我需要在每种情况下获得“value”属性的值?所涉及的XElement
s是add
s,带有XAttribute
skey
和value
。我得到了它-XElement“scheduledtask”包含多个元素“add”,每个元素都带有属性“key”和“value”。是这样吗?所以我需要在每种情况下得到“value”属性的值?这给了我所需要的线索。谢谢,这给了我所需要的线索。谢谢,这给了我所需要的线索。谢谢,这给了我所需要的线索。谢谢