开发者

LINQ to XML selection/update

开发者 https://www.devze.com 2023-02-04 22:19 出处:网络
If I have the following XML, how would I use LINQ to XML blank out the date fields in each video node? I wanted to do it for the purpose of a comparison in a unit test.

If I have the following XML, how would I use LINQ to XML blank out the date fields in each video node? I wanted to do it for the purpose of a comparison in a unit test.

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<main>
<videos>
    <video>
        <id>00000000-0000-0000-0000-000000000000</id>
        <title>Video Title</title>
        <videourl>http://sample.com</videourl>
     开发者_如何转开发   <thumbnail>http://sample.com</thumbnail>
        <dateCreated>2011-01-12T18:54:56.7318386-05:00</dateCreated>
        <dateModified>2011-02-12T18:54:56.7318386-05:00</dateModified>
        <Numbers>
            <Number>28</Number>
            <Number>78</Number>
        </Numbers>
    </video>
    <video>
        <id>00000000-0000-0000-0000-000000000000</id>
        <title>Video Title</title>
        <videourl>http://sample.com</videourl>
        <thumbnail>http://sample.com</thumbnail>
        <dateCreated>2011-01-12T18:54:56.7318386-05:00</dateCreated>
        <dateModified>2011-02-12T18:54:56.7318386-05:00</dateModified>
        <Numbers>
            <Number>28</Number>
            <Number>78</Number>
        </Numbers>
    </video>
</videos>


If you just want to clear the contents of the nodes:

// Load the XML document
XDocument doc = ...;

// Select the date nodes
var query = doc.Descendants()
               .Where(e => e.Name.LocalName.StartsWith("date"));

// Clear the contents of each
foreach (var element in query)
{
    element.SetValue(String.Empty);
}

// Optionally write it back
doc.Save(...);

Yields:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<main>
  <videos>
    <video>
      <id>00000000-0000-0000-0000-000000000000</id>
      <title>Video Title</title>
      <videourl>http://sample.com</videourl>
      <thumbnail>http://sample.com</thumbnail>
      <dateCreated></dateCreated>
      <dateModified></dateModified>
      <Numbers>
        <Number>28</Number>
        <Number>78</Number>
      </Numbers>
    </video>
    <video>
      <id>00000000-0000-0000-0000-000000000000</id>
      <title>Video Title</title>
      <videourl>http://sample.com</videourl>
      <thumbnail>http://sample.com</thumbnail>
      <dateCreated></dateCreated>
      <dateModified></dateModified>
      <Numbers>
        <Number>28</Number>
        <Number>78</Number>
      </Numbers>
    </video>
  </videos>
</main>

Unfortunately it will modify all date nodes no matter where they are in your XML. Personally I'd prefer to use the XPath query in the above if given the option to be very explicit which nodes should be updated. The same can be done using pure LINQ to XML, but it's not as elegant as this.

var xpath = "/main/videos/video/*[starts-with(name(.),'date')]";
var query = doc.XPathSelectElements(xpath);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号