Solved

Help with loading xml file.

Posted on 2011-09-29
7
182 Views
Last Modified: 2012-05-12
Hello,

I am using two different approaches to load the same xml file to accomodate two separate features, is there a way to only load it once?

a. Dim MyReceiver As XDocument = XDocument.Load(Server.MapPath("~/App_Data/Receiver.xml"))
b. Dim dom As New XmlDocument()
        dom.Load(Server.MapPath("~/App_Data/Receiver.xml"))

When using the first approach (a), i can't use MyReceiver.SelectNodes("//ReceiverTable")
 in my statement.

'Receiver
        Dim MyReceiver As XDocument = XDocument.Load(Server.MapPath("~/App_Data/Receiver.xml"))
        Dim LargestReceiverID = (From el In MyReceiver.Descendants("ReceiverTable") Select CInt(el.<Receiver_ID>.Value)).ToList
        ' LargestReceiverID.Sort()
        Dim CheckForItem = (From el In MyReceiver.Descendants("ReceiverTable").Elements("Receiver") Select el.Value).ToList
        If Not CheckForItem.Contains(Me.TextBox2.Text.ToString) Then
            Dim xNew As XElement = New XElement("ReceiverTable")
            xNew.Add(New XElement("Receiver_ID", (LargestReceiverID.Last() + 1)))
            xNew.Add(New XElement("Receiver", Me.TextBox2.Text.ToString))
            MyReceiver.Root.Add(xNew)
            MyReceiver.Save(Server.MapPath("~/App_Data/Receiver.xml"))
        End If
        Dim search As String = TextBox2.Text
        Dim searchId As String = Nothing
        Dim dom As New XmlDocument()
        dom.Load(Server.MapPath("~/App_Data/Receiver.xml"))
        Dim list As XmlNodeList = dom.SelectNodes("//ReceiverTable")
        If CheckForItem.Contains(Me.TextBox2.Text.ToString) Then
            For Each node As XmlNode In list
                If node("Receiver").InnerText.Equals(search) Then
                    searchId = node("Receiver_ID").InnerText
                    Exit For
                End If
            Next
        End If
0
Comment
Question by:vcharles
  • 3
  • 3
7 Comments
 
LVL 14

Expert Comment

by:binaryevo
ID: 36815904
Try using this or a variant of this...  Linq makes it much cleaner.

string filePath = HttpContext.Current.Server.MapPath(SettingsModel.XMLFileLocation + "Moody.xml");
            if (File.Exists(filePath))
            {
                var xml = (from x in XElement.Load(filePath).Elements("Moody")
                           select new XMLModel
                           {
                               ID = x.Element("ID").Value,
                               Value = x.Element("Value").Value
                           });
                return xml;
            }
            else
            {
                throw new Exception("File Path is invalid or file doesnt exist.");
            }
0
 

Author Comment

by:vcharles
ID: 36815978
Can you please send me the code in VB.NET.

Thanks,

Victor
0
 
LVL 14

Accepted Solution

by:
binaryevo earned 500 total points
ID: 36816394
It would look something like this:

 
Dim filePath As String = "Moody.xml"

        If System.IO.File.Exists(FilePath) Then

            Dim xml = From x In XElement.Load(filePath).Elements("Moody")
                                   Select New XMLModel
                                   {
                                       ID = x.Element("ID").Value,
            Value = x.Element("Value").Value
                                   }


        Else
                'Handle your error here
        End If

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 14

Expert Comment

by:binaryevo
ID: 36816399
Be sure to turn Option Infer On also
0
 
LVL 4

Expert Comment

by:theHollow
ID: 36816504
Hi
I haven't verified this, but you may wanna try going via the "DocumentElement" on the XmlDocument.

Dim dom As XmlDocument
Dim root As XmlNode

dom = New XmlDocument()
dom.Load(Server.MapPath("~/App_Data/Receiver.xml"))
root = dom.DocumentElement

Open in new window


Now, you may be able to use "root.SelectNodes("//ReceiverTable")"

Hope it helped :-)
0
 

Author Comment

by:vcharles
ID: 36817118
Thanks, I will try it and get back to you.
0
 

Author Comment

by:vcharles
ID: 36817157
Hi,

I;m a bit confused, do I need to replace option a below with your code?

a. Dim MyReceiver As XDocument = XDocument.Load(Server.MapPath("~/App_Data/Receiver.xml"))
b. Dim dom As New XmlDocument()
        dom.Load(Server.MapPath("~/App_Data/Receiver.xml"))

How would it impact the rest of the code related to the code?

  Dim LargestReceiverID = (From el In MyReceiver.Descendants("ReceiverTable") Select CInt(el.<Receiver_ID>.Value)).ToList
        ' LargestReceiverID.Sort()
        Dim CheckForItem = (From el In MyReceiver.Descendants("ReceiverTable").Elements("Receiver") Select el.Value).ToList
        If Not CheckForItem.Contains(Me.TextBox2.Text.ToString) Then
            Dim xNew As XElement = New XElement("ReceiverTable")
            xNew.Add(New XElement("Receiver_ID", (LargestReceiverID.Last() + 1)))
            xNew.Add(New XElement("Receiver", Me.TextBox2.Text.ToString))
            MyReceiver.Root.Add(xNew)
            MyReceiver.Save(Server.MapPath("~/App_Data/Receiver.xml"))
        End If
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now