【问题标题】:How to use linq to parse xml file?如何使用 linq 解析 xml 文件?
【发布时间】:2014-06-06 20:12:25
【问题描述】:

这是我的 xml 文件:-

<?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>

这是我的 c# 代码:-

    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);

我正在尝试使用 xdocument 类读取 xml 文件。但无法找到为什么我的服务器计数始终为零。代码不会抛出任何错误,但根本不会读取 xml 数据。 请指教?

【问题讨论】:

  • 你应该在代码周围放置一个try-catch,你会很容易在调试器中看到错误。

标签: c# xml linq linq-to-xml xmlreader


【解决方案1】:

你的代码有两个问题,

  1. 首先它是Ftpserver 而不是FtpServer,如您的代码中所示。 (注意小写服务器)
  2. 其次,您正在使用NameUsePassive

排队:

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

应该是UsePassive

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

最后,虽然不是报错,但你不需要.ToList&lt;FtpInfo&gt;();,只要ToList就够了。

所以你的陈述应该是:

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();

【讨论】:

    【解决方案2】:

    UsePassive 的布尔转换失败,请从 Name 更改为 UsePassive

    【讨论】:

      【解决方案3】:

      您的 Xml 节点是“Ftpserver”,但您正在查询“FtpServer”。因此,查询返回零个结果元素。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-13
        • 1970-01-01
        • 1970-01-01
        • 2021-08-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多