By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,666 Members | 1,909 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,666 IT Pros & Developers. It's quick & easy.

Update/Delete/Insert data from XML

P: n/a
Hi all,

I've been though many posts in this group and have yet to find one that
helps me understand how to accomplish my goal.

I have an XML document that stores user specific values that I need to
maintain based on options they select in a web app. Here's the XML:

<?xml version="1.0" encoding="utf-8" ?>
<MyConfig>
<UserInfo>
<UserName>Joe Schmoe</UserName>
<ReportAttributes>
<ReqdFields>
<Field Name="Address"/>
<Field Name="City" />
<Field Name="Close_Date" />
<Field Name="FName1" />
<Field Name="FName2" />
<Field Name="Project" />
<Field Name="State" />
<Field Name="Zip" />
</ReqdFields>
</ReportAttributes>
</UserInfo>
</MyConfig>

A form in the web app displays all available fields that the user can select
as a valid 'ReqdFields' option or remove them from teh list as well. How
can I update the XML and remove or add the updated fields via .NET code to
reflect the changes??

TIA
-Rich
Nov 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Here's a little progress, I just need the touchdown hint!!

-------------------------Begin Code-------------------------

Dim xmlConfig As New XmlDocument
xmlConfig.Load("http://localhost/myapp/Config.xml")

Dim nodeList As XmlNodeList =
xmlConfig.SelectNodes("//myappInfo/UserName")
Dim node As XmlNode
Dim listItem As ListItem

For Each node In nodeList
If node.InnerXml = Me.ddlDivision.SelectedItem.Text Then
node.Attributes.RemoveAll() 'Remove current attributes of
selected user and re-populate
For Each listItem In Me.listSelected.Items
Dim newElem As XmlElement =
xmlConfig.CreateElement("Field")
Dim newAttrib As XmlAttribute =
xmlConfig.CreateAttribute("Name")
newAttrib.Value = listItem.Text
newElem.Attributes.Append(newAttrib)
xmlConfig.DocumentElement.AppendChild(newElem)
Next
End If
Next

-------------------------EndCode-------------------------

Here's the end result:
<myappConfig>
<UserInfo>
<DivisionName>Northern California</DivisionName>
<DivisionCode>SJ</DivisionCode>
<ReportAttributes>
<ReqdFields/>
</ReportAttributes>
</UserInfo>
<UserInfo>
<DivisionName>Tampa</DivisionName>
<DivisionCode>TF</DivisionCode>
<ReportAttributes>
<ReqdFields/>
</ReportAttributes>
</UserInfo>
<Field Name="Project"/>
<Field Name="Builder"/>
</myappConfig>

So...now that I'm able to add the new elements "Field" and attributes
"Name", how can I get this data into the correct ReqdFields node based on
the selected DivisionName in the app??

TIA
-Rich

"Rich Wallace" <ri**********@minusthecannedmeat.jfsheadotcom> wrote in
message news:eP*************@TK2MSFTNGP11.phx.gbl...
Hi all,

I've been though many posts in this group and have yet to find one that
helps me understand how to accomplish my goal.

I have an XML document that stores user specific values that I need to
maintain based on options they select in a web app. Here's the XML:

<?xml version="1.0" encoding="utf-8" ?>
<MyConfig>
<UserInfo>
<UserName>Joe Schmoe</UserName>
<ReportAttributes>
<ReqdFields>
<Field Name="Address"/>
<Field Name="City" />
<Field Name="Close_Date" />
<Field Name="FName1" />
<Field Name="FName2" />
<Field Name="Project" />
<Field Name="State" />
<Field Name="Zip" />
</ReqdFields>
</ReportAttributes>
</UserInfo>
</MyConfig>

A form in the web app displays all available fields that the user can select as a valid 'ReqdFields' option or remove them from teh list as well. How
can I update the XML and remove or add the updated fields via .NET code to
reflect the changes??

TIA
-Rich

Nov 12 '05 #2

P: n/a
Help!

"Rich Wallace" <ri**********@minusthecannedmeat.jfsheadotcom> wrote in
message news:eC****************@TK2MSFTNGP09.phx.gbl...
Here's a little progress, I just need the touchdown hint!!

-------------------------Begin Code-------------------------

Dim xmlConfig As New XmlDocument
xmlConfig.Load("http://localhost/myapp/Config.xml")

Dim nodeList As XmlNodeList =
xmlConfig.SelectNodes("//myappInfo/UserName")
Dim node As XmlNode
Dim listItem As ListItem

For Each node In nodeList
If node.InnerXml = Me.ddlDivision.SelectedItem.Text Then
node.Attributes.RemoveAll() 'Remove current attributes of
selected user and re-populate
For Each listItem In Me.listSelected.Items
Dim newElem As XmlElement =
xmlConfig.CreateElement("Field")
Dim newAttrib As XmlAttribute =
xmlConfig.CreateAttribute("Name")
newAttrib.Value = listItem.Text
newElem.Attributes.Append(newAttrib)
xmlConfig.DocumentElement.AppendChild(newElem)
Next
End If
Next

-------------------------EndCode-------------------------

Here's the end result:
<myappConfig>
<UserInfo>
<DivisionName>Northern California</DivisionName>
<DivisionCode>SJ</DivisionCode>
<ReportAttributes>
<ReqdFields/>
</ReportAttributes>
</UserInfo>
<UserInfo>
<DivisionName>Tampa</DivisionName>
<DivisionCode>TF</DivisionCode>
<ReportAttributes>
<ReqdFields/>
</ReportAttributes>
</UserInfo>
<Field Name="Project"/>
<Field Name="Builder"/>
</myappConfig>

So...now that I'm able to add the new elements "Field" and attributes
"Name", how can I get this data into the correct ReqdFields node based on
the selected DivisionName in the app??

TIA
-Rich

"Rich Wallace" <ri**********@minusthecannedmeat.jfsheadotcom> wrote in
message news:eP*************@TK2MSFTNGP11.phx.gbl...
Hi all,

I've been though many posts in this group and have yet to find one that
helps me understand how to accomplish my goal.

I have an XML document that stores user specific values that I need to
maintain based on options they select in a web app. Here's the XML:

<?xml version="1.0" encoding="utf-8" ?>
<MyConfig>
<UserInfo>
<UserName>Joe Schmoe</UserName>
<ReportAttributes>
<ReqdFields>
<Field Name="Address"/>
<Field Name="City" />
<Field Name="Close_Date" />
<Field Name="FName1" />
<Field Name="FName2" />
<Field Name="Project" />
<Field Name="State" />
<Field Name="Zip" />
</ReqdFields>
</ReportAttributes>
</UserInfo>
</MyConfig>

A form in the web app displays all available fields that the user can

select
as a valid 'ReqdFields' option or remove them from teh list as well. How can I update the XML and remove or add the updated fields via .NET code to reflect the changes??

TIA
-Rich


Nov 12 '05 #3

P: n/a
"Rich Wallace" <ri**********@minusthecannedmeat.jfsheadotcom> wrote in message news:<OV**************@TK2MSFTNGP14.phx.gbl>...
Help!
Dim xmlConfig As New XmlDocument
xmlConfig.Load("http://localhost/myapp/Config.xml")

Dim nodeList As XmlNodeList =
xmlConfig.SelectNodes("//myappInfo/UserName")
Dim node As XmlNode
Dim listItem As ListItem
<myappConfig>
<UserInfo>
<DivisionName>Northern California</DivisionName>
<DivisionCode>SJ</DivisionCode>
<ReportAttributes>
<ReqdFields/>
</ReportAttributes>
</UserInfo>
<UserInfo>
<DivisionName>Tampa</DivisionName>
<DivisionCode>TF</DivisionCode>
<ReportAttributes>
<ReqdFields/>
</ReportAttributes>
</UserInfo>
<Field Name="Project"/>
<Field Name="Builder"/>
</myappConfig>
You need to go one deeper in your select nodes so you end up at the
right division (if I am reading your question correctly): Like this:
xmlConfig.SelectNodes("//myappInfo/UserName")


Should be

xmlConfig.SelectNodes("//myappinfo/UserName[DivisionName = 'Tampa']"

That will get you only the Tampa nodes, just replace Tampa with your
variable and you are homne free.

_Randal
Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.