如何在XML中找到唯一的元素并更改C#中的值?

如何在XML中找到唯一的元素并更改C#中的值?,c#,xml,C#,Xml,我有一个大约67K行的巨大XML。 我不知道参数的确切值。 我只知道哪个包含所需的及其唯一名称 <Configurations> <Configuration type="A"> <Configuration type="B"> <Configuration type="C"> <Section name="A">...</Section> <Section name="B">...</Section&g

我有一个大约67K行的巨大XML。 我不知道参数的确切值。 我只知道哪个
包含所需的
及其唯一名称

<Configurations>

<Configuration type="A">
<Configuration type="B">
<Configuration type="C">
<Section name="A">...</Section>
<Section name="B">...</Section>
<Section name="C">
<Parameter name="a" value="1" />
<Parameter name="specialStuff" value="this" />
</Section>
<Section name="D">...</Section>
...
</Configuration>
</Configuration>
</Configuration>
...

</Configurations>
</Document>
XML看起来类似于:

<Configurations>

<Configuration type="A">
<Configuration type="B">
<Configuration type="C">
<Section name="A">...</Section>
<Section name="B">...</Section>
<Section name="C">
<Parameter name="a" value="1" />
<Parameter name="specialStuff" value="this" />
</Section>
<Section name="D">...</Section>
...
</Configuration>
</Configuration>
</Configuration>
...

</Configurations>
</Document>

...
...
...
...
...

如何访问
specialStuff
参数并修改其值

您可以使用
XDocument
SetAttributeValue()

<Configurations>

<Configuration type="A">
<Configuration type="B">
<Configuration type="C">
<Section name="A">...</Section>
<Section name="B">...</Section>
<Section name="C">
<Parameter name="a" value="1" />
<Parameter name="specialStuff" value="this" />
</Section>
<Section name="D">...</Section>
...
</Configuration>
</Configuration>
</Configuration>
...

</Configurations>
</Document>

您可以使用
XDocument
SetAttributeValue()

<Configurations>

<Configuration type="A">
<Configuration type="B">
<Configuration type="C">
<Section name="A">...</Section>
<Section name="B">...</Section>
<Section name="C">
<Parameter name="a" value="1" />
<Parameter name="specialStuff" value="this" />
</Section>
<Section name="D">...</Section>
...
</Configuration>
</Configuration>
</Configuration>
...

</Configurations>
</Document>
您可以使用XPath

<Configurations>

<Configuration type="A">
<Configuration type="B">
<Configuration type="C">
<Section name="A">...</Section>
<Section name="B">...</Section>
<Section name="C">
<Parameter name="a" value="1" />
<Parameter name="specialStuff" value="this" />
</Section>
<Section name="D">...</Section>
...
</Configuration>
</Configuration>
</Configuration>
...

</Configurations>
</Document>
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\Yourxml.xml");

XmlElement root = doc.DocumentElement; 
string qry = "//Parameter[@name='specialStuff']"; 
XmlNode node = root.SelectSingleNode(qry);
node.Attributes["value"].Value = "newValue";

doc.Save(@"D:\Yourxml.xml");
您可以使用XPath

<Configurations>

<Configuration type="A">
<Configuration type="B">
<Configuration type="C">
<Section name="A">...</Section>
<Section name="B">...</Section>
<Section name="C">
<Parameter name="a" value="1" />
<Parameter name="specialStuff" value="this" />
</Section>
<Section name="D">...</Section>
...
</Configuration>
</Configuration>
</Configuration>
...

</Configurations>
</Document>
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\Yourxml.xml");

XmlElement root = doc.DocumentElement; 
string qry = "//Parameter[@name='specialStuff']"; 
XmlNode node = root.SelectSingleNode(qry);
node.Attributes["value"].Value = "newValue";

doc.Save(@"D:\Yourxml.xml");

使用XML读取器。如果超出这个示例,你实际上需要改变一个以上的值,也许可以考虑使用XSL转换,至少你可以量化它。通常,当人们说他们有一个巨大的XML时,我必须问他们的意思是100Mb还是100Gb。如果超出这个示例,你实际上需要改变一个以上的值,也许可以考虑使用XSL转换,至少你可以量化它。通常,当人们说他们有一个巨大的XML时,我不得不问他们是指100Mb还是100Gb。