close

運用 XElement  有兩種方式 可以 將 Node 的資料刪除


Dim  Xdoc as Xelement = Xelement.Load( "C:\text.xml")


比如  想要將  

<Recipe>
    <RcpNo>1011</RcpNo>
    <RcpName>123</RcpName>
 </Recipe>

重 節點中鐘移除

1. 法一:  用反選的方式,將 不一樣的挑出來
   XDoc = New XElement(<RecipeList><%= From rcp In XDoc.Elements("Recipe")
                                            Select rcp
                                            Where rcp.Element("RcpNo").Value <> recipe.RcpNo %>
                                       </RecipeList>)



2. 法二: 用正選的方式,在將之移除

  Dim selectXelement As System.Collections.Generic.IEnumerable(Of XElement) = From rcp In Xdoc.Elements("Recipe")
                                                                                        Select rcp
                                                                                        Where rcp.Element("RcpNo").Value = rcpNo.ToString

   selectXelement.Remove()


===================================================
test.xml 內的資料

<?xml version="1.0" encoding="utf-8"?>
<RecipeList>
  <Recipe>
    <RcpNo>1001</RcpNo>
    <RcpName>SEtest2</RcpName>
  </Recipe>
  <Recipe>
    <RcpNo>1002</RcpNo>
    <RcpName>ABCD</RcpName>
  </Recipe>
  <Recipe>
    <RcpNo>1005</RcpNo>
    <RcpName>RStest1</RcpName>
  </Recipe>
  <Recipe>
    <RcpNo>1011</RcpNo>
    <RcpName>123</RcpName>
   </Recipe>
</RecipeList>

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Rh 的頭像
    Rh

    程式狂想曲

    Rh 發表在 痞客邦 留言(0) 人氣()