Solved

Help with loading xml file.

Posted on 2011-09-29
7
187 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 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…

830 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