• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 604
  • Last Modified:

read xml file in C#

Do you see anything wrong in my code? I am trying to read xml file. It does not like my foreach.

public string Get_Config_valuex(string Key_NAME)
            string result;
            XDocument xdoc = new XDocument();
            using (FileStream instream = new FileStream(@"E:\\xmlfile.config", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                 using (StreamReader reader = new StreamReader(instream))
                     xdoc = XDocument.Load(reader); 
                     var ArrResults = from c in xdoc.Descendants("ADD")
                     foreach (XElement Each_Results in ArrResults)
                      if (Each_Results.Attribute("key") == (Key_NAME.ToUpper))
                            result = Each_Results.Attribute("VALUE").Value;    

Open in new window

1 Solution
Fernando SotoRetiredCommented:
Hi VBdotnet2005;

First you are most likely getting an exception that states you must have a select or a group clause at the end. Please post the exact exception message and inner exception message that you are receiving. Please also describe what you want the end result to look like and it would help to see what the XML document looks like so please post.

Second you can load the XML document in a less verbose way for example:

XDocument xdoc = XDocument.Load("E:/xmlfile.config");
Jitendra PatilSr.Software EngineerCommented:
hi you  can try the below links
How to read XML from a file by using Visual Basic .NET
HOW TO: Read XML Data into a DataSet by Using Visual Basic .NET
OR try the below code
Imports System.Xml
Imports System.Data
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xmlFile As XmlReader
        xmlFile = XmlReader.Create("Product.xml", New XmlReaderSettings())
        Dim ds As New DataSet
        Dim i As Integer
        For i = 0 To ds.Tables(0).Rows.Count - 1
    End Sub
End Class
hope this helps.
Carl TawnSystems and Integration DeveloperCommented:
As Fernando alluded to, you appear to be missing the select clause in your Linq:
var ArrResults = from c in xdoc.Descendants("ADD")
                          select c;

Open in new window

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

VBdotnet2005Author Commented:
This is my xml format. I just want it to return value = "mystring"

<?xml version="1.0" encoding="utf-8"?>
    <ADD key ="server1" VALUE="mystring" />
    <ADD key="server2" VALUE="mystring" />
Fernando SotoRetiredCommented:
Hi VBdotnet2005;

Well then this should work for you.

// Load the xml document into a XDocument node from a file on the system
XDocument xdoc = XDocument.Load("C:/Working Directory/MyConfig.xml");
// Or if the XML is already in memory as a string you can load it this way 
// for example assuming the variable name is myXml
XDocument xdoc = XDocument.Parse(myXml);

// Query for the ADD nodes and return the VALUE attribute for each
var results = from a in xdoc.Descendants("ADD")
              select a.Attribute("VALUE").Value;
foreach (string val in results)
	  // Write out the value

Open in new window

VBdotnet2005Author Commented:
Thank you Fernando
Fernando SotoRetiredCommented:
Not a problem, glad to help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now