1

私のxmlファイル:

<?xml version="1.0" encoding="utf-8"?>
<layout name="layout">
  <section name="Header">
    <placeholder name="headers" width="30" class="header">sam,pam</placeholder>
  </section>
  <section name="Content">
    <placeholder name="RightA" width="55">location</placeholder>
  </section>
</layout>

ノードが含まれている場合はノード全体を置き換えたい。samノードが含まれている場合はノードsamを書き換えたい:

<placeholder name="headers" width="4,5,91">sam,sam2,pam</placeholder>

それ以外の:

<placeholder name="headers" width="30" class="header">sam,pam</placeholder>

C#の場合:

XmlDocument doc = new XmlDocument();
string sFileName = @"FileNameWithPath";
doc.Load(sFileName );
foreach (XmlNode ....... )
{
    //Need help hear how to loop and replace.
}

ありがとう。

4

2 に答える 2

0
XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.Load("Path");
 XmlNodeList nodeList = xmlDoc.SelectNodes("section") ;

 foreach (XmlNode node in nodeList)
   {
      XmlNode childNode = node.SelectSingleNode("placeholder");
        if (childNode.Value.Contains("sam"))
          {
              childNode.Value = "sam,pam,sam2";
              childNode.Attributes["width"].Value = "4,5,91";

           }
   }

 xmlDoc.Save("Path");
于 2012-11-01T07:45:08.143 に答える
0

XDocumentを使用して、検索と置換をより適切に制御してみてください。

XDocument myDocument = XDocument.Load("path to my file");
foreach (XElement node in myDocument.Root.Descendants("placeholder"))
{
    if (node.Value.Contains("same"))
    {
        XElement newNode = new XElement("placeholder");
        newNode.Add(new XAttribute("header", node.Attribute("header").Value); // if you want to copy the current value
        newNode.Add(new XAttribute("width", "some new value"));
        node.ReplaceWith(newNode);
    }
}
于 2012-11-01T07:20:53.723 に答える