用LINQ快速改变XML元素的值

Categories: Other
Comments: 11 Comments
Published on: 2008 年 09 月 27 日

[原文作者]:Beth Massi

[原文链接]:Quickly Changing Values of XML Elements Using LINQ

    最近我在考虑一些关于如何使用LINQ查询XML文档(或片段)中一个特定节点并且改变其值的问题(这说明人们已经开始用这种方法了,这很是让我激动)。这是一个很方便的方法:我们可以改变查询返回节点的值,这样源XML中的值也会跟着改变。

   一个例子:

   Imports <xmlns="urn:mycompany:examples:plants">

   Module Module1

    Sub Main()

        Dim plants = <?xml version="1.0" encoding="ISO-8859-1"?>

                     <CATALOG xmlns="urn:mycompany:examples:plants">

                         <PLANT>

                             <COMMON>Bloodroot</COMMON>

                             <BOTANICAL>Sanguinaria canadensis</BOTANICAL>

                             <ZONE>4</ZONE>

                             <LIGHT>Mostly Shady</LIGHT>

                             <PRICE>$2.44</PRICE>

                             <AVAILABILITY>031599</AVAILABILITY>

                         </PLANT>

                         <PLANT>

                             <COMMON>Columbine</COMMON>

                             <BOTANICAL>Aquilegia canadensis</BOTANICAL>

                             <ZONE>3</ZONE>

                             <LIGHT>Mostly Shady</LIGHT>

                             <PRICE>$9.37</PRICE>

                             <AVAILABILITY>030699</AVAILABILITY>

                         </PLANT>

                         <PLANT>

                             <COMMON>Marsh Marigold</COMMON>

                             <BOTANICAL>Caltha palustris</BOTANICAL>

                             <ZONE>4</ZONE>

                             <LIGHT>Mostly Sunny</LIGHT>

                             <PRICE>$6.81</PRICE>

                             <AVAILABILITY>051799</AVAILABILITY>

                         </PLANT>

                     </CATALOG>

 

        Dim q = From plant In plants…<PLANT> _

                Where plant.<COMMON>.Value = "Columbine" _

                Select plant

 

        For Each item In q

            q.<PRICE>.Value = "$49.99"

            q.<LIGHT>.Value = "Full Sun"

        Next

 

        plants.Save("plants.xml")

    

    End Sub

 

 End Module

    注意两点:记得导入XML用到的所有命名空间,否则查询会返回空值;如果导入了构架(这个很容易就能办到,请参考这里)记得用XML智能提示。当然,我们也可以不把XML的内容全部写在代码里,而是从文件或者通过URI加载,这样可以得到一样的结果:

    Dim plants = XDocument.Load("plants.xml")

    Dim q = From plant In plants…<PLANT> _

        Where plant.<COMMON>.Value = "Columbine" _

        Select plant

 

    For Each item In q

    q.<PRICE>.Value = "$49.99"

    q.<LIGHT>.Value = "Full Sun"

    Next

 

    plants.Save("plants.xml")

   在这个例子中,我们用新的值覆盖源文档plants.xml。上文的两个例子产生同样的结果:

   <?xmlversion="1.0"encoding="iso-8859-1"?>

   <CATALOGxmlns="urn:mycompany:examples:plants">

    <PLANT>

    <COMMON>Bloodroot</COMMON>

    <BOTANICAL>Sanguinaria canadensis</BOTANICAL>

    <ZONE>4</ZONE>

    <LIGHT>Mostly Shady</LIGHT>

    <PRICE>$2.44</PRICE>

    <AVAILABILITY>031599</AVAILABILITY>

 </PLANT>

 <PLANT>

    <COMMON>Columbine</COMMON>

    <BOTANICAL>Aquilegia canadensis</BOTANICAL>

    <ZONE>3</ZONE>

    <LIGHT>Full Sun</LIGHT>

    <PRICE>$49.99</PRICE>

    <AVAILABILITY>030699</AVAILABILITY>

 </PLANT>

 <PLANT>

    <COMMON>Marsh Marigold</COMMON>

    <BOTANICAL>Caltha palustris</BOTANICAL>

    <ZONE>4</ZONE>

    <LIGHT>Mostly Sunny</LIGHT>

    <PRICE>$6.81</PRICE>

    <AVAILABILITY>051799</AVAILABILITY>

 </PLANT>

</CATALOG>

 

 

 

 

11 Comments - Leave a comment
  1. Rattling clean internet site , appreciate it for this post.

  2. I’m not sure where you are getting your info, but good topic. I needs to spend some time learning much more or understanding more. Thanks for magnificent info I was looking for this info for my mission.

  3. Great work! This is the type of information that should be shared around the internet. Shame on Google for not positioning this post higher! Come on over and visit my website . Thanks =)

  4. varicella说道:

    Oh my goodness! an amazing article dude. Thank you However I’m experiencing difficulty with ur rss . Don’t know why Unable to subscribe to it. Is there anybody getting similar rss problem? Anyone who knows kindly respond. Thnkx

  5. Real instructive and superb bodily structure of subject matter, now that’s user genial (:.

  6. Jerry Hilke说道:

    I carry on listening to the rumor speak about receiving boundless online grant applications so I have been looking around for the top site to get one. Could you tell me please, where could i acquire some?

  7. Czat说道:

    Great post. I was checking continuously this blog and I’m impressed! Extremely useful info particularly the last part :) I care for such information a lot. I was seeking this certain information for a long time. Thank you and best of luck.

  8. It’s laborious to seek out knowledgeable people on this topic, however you sound like you recognize what you’re talking about! Thanks

  9. Chieko Timmis说道:

    I really like the way you write and post your ads for free on adultadsusa.com.

  10. Juli Dechambeau说道:

    As a Newbie, I am constantly searching online for articles that can help me. Thank you

Leave a comment


Welcome , today is 星期六, 2017 年 02 月 25 日