Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with removing data from an xml file.

How do I loop through  both Item and LinkTotalA.xml files and delete ItemTable element from Item.xml  if the Item_ID in LinkTotalA.xml is not found in Item.xml?
.Item.xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ItemTable>
<Item_ID>1</Item_ID>
<Item>BMW</Date_ID>
</ItemTable>
<ItemTable>
<Item_ID>2</Item_ID>
<Item>Toyota</Date_ID>
</ItemTable>
<ItemTable>
<Item_ID>3</Item_ID>
<Item>Honda</Date_ID>
</ItemTable>
</Root>


LinkTotalA.xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<LinkTotalTable>
<LinkTotal_ID>1</LinkTotal_ID>
<Location_ID>3</LocationA_ID>
<Item_ID>1</Item_ID>
</LinkTotalTable>
<LinkTotalTable>
<LinkTotal_ID>2</LinkTotal_ID>
<Location_ID>2</LocationA_ID>
<Item_ID>1</Item_ID>
</LinkTotalTable>
<LinkTotalTable>
<LinkTotal_ID>3</LinkTotal_ID>
<Location_ID>2</LocationA_ID>
<Item_ID>2</Item_ID>
</LinkTotalTable>

Give the example of xml files above, after running the code my Item.xml should be as follows:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ItemTable>
<Item_ID>1</Item_ID>
<Item>BMW</Date_ID>
</ItemTable>
<ItemTable>
<Item_ID>2</Item_ID>
<Item>Toyota</Date_ID>
</ItemTable>
</Root>

Becuase Item_ID = 3 was not found in the LinkTotalA.xml file.

Thanks,

V.
Avatar of Theo Kouwenhoven
Theo Kouwenhoven
Flag of Netherlands image

Hi  vcharles,

inXml => Process => outXml

XML is a transportmedium, not a DB table. so preferable you have to put the xml in tables do your job and create new XML files. If you draw the process, it's clear that this is the way.
Changing data directly in xml without a DB process, is actually the same a removeing the red pixels from a *.bmp without paint. It's doable but not the right way.
Avatar of Victor  Charles

ASKER

Unfortunately, I am only allowed to use XML, how do  i modify the code below to go through all the records in LinkTotal.xml and compare to each record in Item.xml and if you arrive at the last record in LinkTotal.xml and there is no match with Item_ID then delete the record in Item.xml.

Code below only loops through Item.xml, need to include portion which loops through LinkTotal.xml.        

Dim dkF As New XmlDocument
        dkF.Load(Server.MapPath("~/App_DataA/Item.xml"))
        Dim pizF As XmlElement = dkF.DocumentElement
        For Each topping As Xml.XmlNode In pizF.SelectNodes("/Root/ItemTable[Item_ID<>'" & LinkTotal_ID & "']")
            topping.ParentNode.RemoveChild(topping)
            dkF.Save(Server.MapPath("~/App_DataA/Item.xml"))
            Exit For
        Next
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, I will test it and get back to you.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.