Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with loading xml file.

Posted on 2011-09-29
7
Medium Priority
?
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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…
Introduction to Processes

670 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