Click here to Skip to main content
15,911,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 xml config files contains either:

1 :<User Server="server name" Database="database name" />

2: <User Server="server name" Database="database name" Login="login" Password="password" ReportOpenType="Preview" />

I just want to be able to extract the Server Name from the User Server="server name"

note that I have used server name etc for data protection reasons.

I would really appreciate a rapid response.

What I have tried:

I have tried this
C#
XDocument xDoc = XDocument.Load(exepath);
            //xDoc.
            string nodeValue = string.Empty;
var node = xDoc.Descendants().Where(n => n.Name == nodeName).FirstOrDefault();



                        if (node != null)
                        {
                            nodeValue = node.Value;
                        }


this works on others which have
<server>server name</server>
, but not where there is <User Server="server name" as described above.
Posted
Updated 27-Jul-17 6:42am

Instead of getting a value, try to get the attribute:
C#
string value = xDoc.Root.Element("User").Attribute("Server").Value;
 
Share this answer
 
v2
Have you try the Attribute method? I don't know what in the XML file, assuming this is the only line in the XML file.

XML
<User Server="server name" Database="database name" Login="login" Password="password" ReportOpenType="Preview" />

Here is the code to get the Server value
C#
var serverName = (from s in xDoc.Descendants("User")
                           select s.Attribute("Server").Value).FirstOrDefault ();


Output:
server name
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900