0

Here i my xml file:-

<?xml version="1.0" encoding="utf-8" ?>
<Ftpservers>
  <Ftpserver>
    <Name>Client1</Name>
    <ServerIP>10.10.10.100:1961</ServerIP>
    <UserName>username</UserName>
    <Password>pa$#word1</Password>
    <EnableSSL>false</EnableSSL>
    <UsePassive>false</UsePassive>
</Ftpserver>
  <Ftpserver>
    <Name>Client2</Name>
    <ServerIP>10.10.10.101:1961</ServerIP>
    <UserName>username</UserName>
    <Password>pa$#word1</Password>
    <EnableSSL>false</EnableSSL>
    <UsePassive>false</UsePassive>
  </Ftpserver>
</Ftpservers>

and here is my c# code:-

    public class FtpInfo
        {
            public string Name { get; set; }
            public string ServerIp { get; set; }
            public string UserName { get; set; }
            public string Password { get; set; }
            public bool EnableSsl { get; set; }
            public bool UsePassive { get; set; }
        }

var xmlReader = XDocument.Load("FtpDestination.xml");
var servers = (from f in xmlReader.Descendants("FtpServer")
            select new FtpInfo
            {
                Name = f.Element("Name").Value,
                ServerIp = f.Element("ServerIP").Value,
                UserName = f.Element("UserName").Value,
                Password = f.Element("Password").Value,
                EnableSsl = Convert.ToBoolean(f.Element("EnableSSL").Value),
                UsePassive = Convert.ToBoolean(f.Element("UsePassive").Value)
            }).ToList<FtpInfo>();
foreach(var server in servers)
    Console.Write(server.Name);

I am trying to read an xml file using xdocument class. but unable to find why my servers count is always zero. Code does not throw any error but not reading xml data at all. Please advice?

1
  • you should put a try-catch around the code and you would see the errors easily in the debugger. Commented Jun 6, 2014 at 20:35

3 Answers 3

3

There are two problems with your code,

  1. First it is Ftpserver not FtpServer as in your code. (notice the lower case server)
  2. Second, You are using Name for UsePassive

In line:

UsePassive = Convert.ToBoolean(f.Element("Name").Value)

It should be UsePassive

UsePassive = Convert.ToBoolean(f.Element("UsePassive").Value)

Last, although it is not an error, but you don't need .ToList<FtpInfo>(); only ToList is enough.

So your statement should be:

var servers = (from f in xmlReader.Descendants("Ftpserver")
               select new FtpInfo
               {
                   Name = f.Element("Name").Value,
                   ServerIp = f.Element("ServerIP").Value,
                   UserName = f.Element("UserName").Value,
                   Password = f.Element("Password").Value,
                   EnableSsl = Convert.ToBoolean(f.Element("EnableSSL").Value),
                   UsePassive = Convert.ToBoolean(f.Element("UsePassive").Value)
               }).ToList();
Sign up to request clarification or add additional context in comments.

Comments

2

UsePassive is failing you in its Boolean conversion, change from Name to UsePassive.

Comments

2

Your Xml nodes are "Ftpserver" but you are querying for "FtpServer". The query therefore returns zero result elements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.