• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

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.
0
vcharles
Asked:
vcharles
1 Solution
 
theo kouwenhovenCommented:
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.
0
 
vcharlesAuthor Commented:
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
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I confess that I am not a .NET coder and I have nothing to check the validity of this code, but at the very least, it should give you an idea about one way to solve your problem...
Dim ltd As New XmlDocument
ltd.Load(Server.MapPath("~/App_DataA/LinkTotalA.xml"))
Dim lte As XmlElement = ltd.DocumentElement
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")
    if lte.SelectNodes("/Root/LinkTotalTable[Item_ID = '" & topping.SelectSingleNode("Item_ID").InnerText & "']").Count == 0 Then
        topping.ParentNode.RemoveChild(topping)
    End If
Next
dkF.Save(Server.MapPath("~/App_DataA/Item.xml"))

Open in new window

0
 
vcharlesAuthor Commented:
Thanks, I will test it and get back to you.
0
 
Amandeep Singh BhullarCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now