Solved

Help with loading xml file.

Posted on 2011-09-29
7
183 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
DataGridView Events ? 3 37
Name space syntax error 12 39
Connection String 16 42
JQuery to parse xml string and get element by id 4 20
A short article about a problem I had getting the GPS LocationListener working.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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 …

932 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

11 Experts available now in Live!

Get 1:1 Help Now