I have the following XML file which has the namespace as shown...

I have to change the inner text of XML file but need suggestion how to do it.

My XML file is:

    <?xml version="1.0"?>
    <Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
      <buttons>
        <workshop1>Google</workshop1>
<workshop1>Yahoo</workshop1>
        <url1>www.google.co.uk</url1>
      </buttons>
    </Report>

I have to change the inner text of second node workshop1 from "Yahoo" to "new".

有帮助吗?

解决方案

Do this using XElement.

The "problem" you probably had is due to the namespace this xml has:

    XElement xml = getXml(); // get your xml from a service\file\ftp etc..
    XNamespace ns = "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition";
    xml.Descendants(ns + "workshop1").First().Value = "new";

Your output:

<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
  <buttons>
    <workshop1>new</workshop1>
    <url1>www.google.co.uk</url1>
  </buttons>
</Report>

For more than one node use this:

    XNamespace ns = "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition";
    var nodes = xml.Descendants(ns + "workshop1").ToList();
    nodes[0].Value = "new";
    nodes[1].Value = "new2"; 
    // etc.... 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top