Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XML VB.NET
I have this xml :
<un:casamae Id="1">
           <un:casafilha Id="1">
            <xn:telhado id="without records">
           </xn:telhado>
                <xn:telhado id="without records" modifier="create">
               <xn:composicao>
                 <xn:tipotelha>vermelho</xn:vsDataType>
                 <xn:numerotelhas>127</xn:vsDataFormatVersion>
                 <es:vermelho />
               </xn:composicao>
             </xn:telhado>
                <xn:muro id=256 modifier="create">
                </xn:muro>
           </un:casafilha>
           <un:casafilha Id="65501">
           </un:casafilha>
 
I want this :
 
<un:casamae Id="1">
           <un:casafilha Id="1">
                <xn:muro id=256 modifier="create">
                </xn:muro>
           </un:casafilha>
           <un:casafilha Id="65501">
           </un:casafilha>
 
This is the code I have so far:
 doc2.Load(folder_dest & "\xpto.xml")
            For Each casafilha In casafihalist
                Dim xpathstring2 = "un:casamae//un:casafilha[@Id=" & casafilha & "]"
                Dim nodeselect2 As XmlNode = doc2.SelectSingleNode(xpathstring2, namespaces)
                For Each node As XmlNode In nodeselect2.SelectNodes("//xn:telhado[@id='without records']", namespaces)             
                    node.ParentNode.RemoveChild(node)
                Next
            Next casafilha
            doc2.Save(folder_dest & "\xpto.xml")
 
The problem:
I've tried several solutions, but so far, I just being able of deleting the first chid xml with the "id" atribute equals "without records", leaving always the one with the id="without records" and modifier="create" behind.
 
How can I delete any kind of telhado id="without records" and their own subelements?
Posted 19-Apr-13 1:10am
Comments
Prasad Khandekar at 19-Apr-13 7:01am
   
You can not have same id for two nodes. id attribute value has to be unique. Your xml also seems to be invalid. (vermelho) "xn:tipotelha" must be terminated by the matching end-tag "", id attribute value is not enclosed in double quotes (id=256)
 
Regards,
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
doc2.SelectSingleNode
 
You're selecting a single node.
 
Must use this function http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.selectnodes.aspx[^] to receive a list of nodes.
 
Good luck!
JAFC
  Permalink  
Comments
etmoreira123 at 19-Apr-13 7:23am
   
Hi
Im already using what you saying,
But again I only have the same result : only delete the first one and forget the other one with the atribute modifier=create.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

doc2.Load(folder_dest & "\xpto.xml")
 
Dim nodeselect2 As XmlNodelist
 
nodeselect2=doc2.SelectNodes("//xn:telhado[@id='without records']", namespaces)
 
For Each node As XmlNode In nodeselect2

doc2.RemoveChild(node)
 
Next

doc2.Save(folder_dest & "\xpto.xml")
  Permalink  
Comments
etmoreira123 at 19-Apr-13 8:47am
   
Ive tried your solution, but all that it gives me is : Object reference not set to an instance of an object - this error on :doc2.RemoveChild(node)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 230
1 OriginalGriff 202
2 Richard MacCutchan 185
3 Tomas Takac 146
4 CPallini 125
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100