473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Elegant way to remove all nodes from XML whose child has text that ISNOT in array

Hi all,

I need help on an elegant way of removing nodes from XML that do not
have children with text that is kept in array.
Here is the example:

I have array of integers = {123, 456}
and an XML like this:
<W>
<U></U>
<P></P>
<S></S>
<LIST>
<INS>
<I>
<L></L>
<F></F>
<ID>123</ID>
</I>
</INS>
<INS>
<I>
<L></L>
<F></F>
<ID>456</ID>
</I>
</INS>
<INS>
<I>
<L></L>
<F></F>
<ID>999</ID>
</I>
</INS>
</LIST>
</W>

I need to remove all <INSnodes whose I/ID child has text that IS NOT
in array. In here it is the last <INSnode because ID text is 999
which is not in array.

Thanks
Nov 10 '08 #1
1 2046
jsmith wrote:
I need help on an elegant way of removing nodes from XML that do not
have children with text that is kept in array.
With .NET 3.5 you can use LINQ to XML as follows:

Dim el As XElement = _
<W>
<U></U>
<P></P>
<S></S>
<LIST>
<INS>
<I>
<L></L>
<F></F>
<ID>123</ID>
</I>
</INS>
<INS>
<I>
<L></L>
<F></F>
<ID>456</ID>
</I>
</INS>
<INS>
<I>
<L></L>
<F></F>
<ID>999</ID>
</I>
</INS>
</LIST>
</W>

Dim a As Integer() = {123, 456}

el...<INS>.Where(Function(ins) Not
(a.Contains(ins.<I>.<ID>.Value))).Remove()

'Save to Console for testing
el.Save(Console.Out)
If you can't use LINQ to XML then try

Dim doc As New XmlDocument()
doc.Load("..\..\XMLFile1.xml")

Dim a As Integer() = {123, 456}

For Each ins As XmlElement In doc.SelectNodes("/W/LIST/INS")
If Not (a.Contains(ins("I")("ID").InnerText)) Then
ins.ParentNode.RemoveChild(ins)
End If
Next

doc.Save(Console.Out)

that should work with .NET 1.0 and later.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 11 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jamie Green | last post by:
Using MSXML3.0, with the Dom SelectionLanguage set to Xpath I am trying to query the following document <Root> <Child>Name</Child> <Child>John</Child> <Child>Smith</Child> <Child>23</Child>...
3
by: Simon | last post by:
Hi, I have generated an xml document, and would like to be able to transform it to another such that the contents of a chosen node type are flattened (i.e. tags removed). e.g. <shop>...
1
by: Gordon - Adelphia | last post by:
I have a question regarding xhtml. Why, why, why does the ELEMENT <body> allow “unblocked” text. HTML does not (though, most browsers will render). Xhtml (transitional) however allows text nodes...
47
by: Jeff Relf | last post by:
Hi All, I plan on using the following C++ code to create nodes with unlimited children: // I would like to declare NodeT like this, // but it won't compile because Lnk_T is not defined yet....
13
by: kaeli | last post by:
Can anyone explain this to me? It's driving me insane. Save this and run it in IE or Opera and then in Mozilla or Netscape 6+. In IE/Opera, I get the expected 4 alerts. In Mozilla/Netscape, I...
6
by: Nikhil Patel | last post by:
Hi all, Following is a portion of an XML document. I need to remove all nodes that belong to ns0 without deleting their child nodes. So in the following example , I want to delete "ns0:Proposal"...
8
by: Mikey | last post by:
I have an XML document as follows: <Menu> <Group> <Item Text="About Us" AccessRoles="All"> <Group> <Item Text="Option 1" AccessRoles="All" /> <Item Text="Option 2" AccessRoles="All" /> <Item...
3
by: Markus | last post by:
Hi! I wanted to select a subset of nodes (list = selectNodes("parent/child") from a XmlDocument, then remove all (parentNode.removeAll();) child-nodes and insert the previous selected nodes...
11
by: David | last post by:
Hi All, I am working on a script that is theoreticaly simple but I can not get it to work completely. I am dealing with a page spit out by .NET that leaves empty tags in the markup. I need a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.