문제

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