Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with loading xml file.

Posted on 2011-09-29
7
Medium Priority
?
194 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Screencast - Getting to Know the Pipeline

972 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