Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this xml :
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 :

XML
<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
Comments
Prasad Khandekar 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. (<xn:tipotelha xmlns:xn="#unknown">vermelho) "xn:tipotelha" must be terminated by the matching end-tag "", id attribute value is not enclosed in double quotes (id=256)

Regards,

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
 
Share this answer
 
Comments
etmoreira123 19-Apr-13 7:23am    
HiIm 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.
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")
 
Share this answer
 
Comments
etmoreira123 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900