【问题标题】:Reading any xml from physical location从物理位置读取任何 xml
【发布时间】:2014-06-17 15:49:16
【问题描述】:

我必须从物理位置读取任何 xml 文件。 我按照下面的方式做,但是,它说文件现在找到了。

例如,我可以拥有任何文件(a.xml、b.xml、c.xml、..... z.xml)
所以我想用一个通用的代码来读取xml。

有什么帮助吗? 谢谢你

【问题讨论】:

  • 如果有多个文件匹配*.xml,你想要哪个文件内容?全部?

标签: c# asp.net .net vb.net


【解决方案1】:

Load() 的参数应该是一个文件。您可以遍历文件数组以打开文档。

    static void Main(string[] args)
    {
        const string folder = "C:\\";

        // Loop trough all
        foreach (var file in Directory.EnumerateFiles(folder, "*.xml"))
        {
            var document = XDocument.Load(file);
        }

        // When it should explicitly be one
        var singleFile = Directory.GetFiles(folder, "*.xml").SingleOrDefault();
        if (singleFile == null) throw new Exception("File missing or multiple files found");
        var document = XDocument.Load(singleFile);
    }

【讨论】:

    【解决方案2】:

    既然你用 vb.net 标记了这篇文章,这里是一个 vb 答案。

    Dim fileList as new List(of FileInfo)
    dim basepath as string = "drive:\path\to\base\folder"
    
    fileList = new IO.DirectoryInfo(basepath).GetFiles("*.xml")
    
    For each fle in fileList
        Dim xDoc = XDocument.Load(fle.FullName)
        objIntegrationInfo.xmlstring += xDoc.ToString()
    Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 2017-08-17
      • 2012-11-16
      • 1970-01-01
      • 2013-04-09
      相关资源
      最近更新 更多