2017-09-12 10 views
0

たとえば、次のようなxmlファイルがあります。C#を使用して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> 

私はこれらの.meの値を.awesomeに置き換えたいと考えています。

例:<add key="1" val="great.me"/>

<add key="1" val="great.awesome"/>へ例:<add key="3" val="lessGreat.awesome/yey"

から<add key="3" val="lessGreat.me/yey"

はあなたが私の男を助けることができますか? TIA

+0

、これを試してみてください依頼する方法にREFEREしてください - あなたは何を試してみましたか?あなたが直面しているあなたのコードの特定の問題は何ですか?問題を解決するために何をしたのですか? –

+0

[Feeding Vampires](https://meta.stackoverflow.com/a/258208/5427842)みんな! – SeM

答えて

1

string oldText = File.ReadAllText(filePath); 
string newText = oldText.Replace("me", "awesome"); 
File.WriteAllText(filePath, newText, Encoding.UTF8); 
xmlDoc = new XmlDocument(); 
xmlDoc.Load(filePath); 
0
//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"); 
関連する問題