Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用C在XML中编辑特定的子字符串_C#_Xml - Fatal编程技术网

C# 如何使用C在XML中编辑特定的子字符串

C# 如何使用C在XML中编辑特定的子字符串,c#,xml,C#,Xml,例如,我有一个xml文件,如下所示 <file> <add key="1" val="great.me"/> <add key="2" val="notSoGreat"/> <add key="3" val="lessGreat.me/yey" /> <add key="4" val="soGreat/yey" /> </file> 我想取代那些。我要。太棒了 例: 到 及 例:试试这个 string oldTex

例如,我有一个xml文件,如下所示

<file>
 <add key="1" val="great.me"/>
 <add key="2" val="notSoGreat"/>
 <add key="3" val="lessGreat.me/yey" />
 <add key="4" val="soGreat/yey" />
</file>
我想取代那些。我要。太棒了

例:

例:试试这个

string oldText = File.ReadAllText(filePath);
string newText = oldText.Replace("me", "awesome");
File.WriteAllText(filePath, newText, Encoding.UTF8);
xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);

请参考如何提问-你试过什么?代码中您面临的具体问题是什么?你做了些什么来尝试解决这些问题阻止你们!!
    //load xml from file
    XmlDocument doc = new XmlDocument();
    doc.Load(@"E:\test1.xml");

    // get a list of nodes - 
    XmlNodeList allnodes = doc.SelectNodes("/file/add");

    // loop through all nodes
    foreach (XmlNode node in allnodes)
    {
       // get "value"
       XmlAttribute attrValue= node.Attributes["value"];   
       attrValue.value = "something";// use replace here     

    }

    // save new xml or you can overwrite
    doc.Save(@"E:\test1New.xml");