.net 3.5项目中的Linq to XML问题(以前是从.net 2.0转换而来的) [英] Problem with Linq to XML in .net 3.5 project (previously converted from .net 2.0)

查看:67
本文介绍了.net 3.5项目中的Linq to XML问题(以前是从.net 2.0转换而来的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我在.net 3.5中遇到了Linq to XML的一些问题,我希望有人能够帮助我。

I'm experiencing some problems with Linq to XML in .net 3.5 and I hope someone will be able to help me.

我有以下代码:


XDocument xml = XDocument.Load(FileUpload1.PostedFile.FileName);
    
    
                int no_of_rows = xml.Root.Descendants("{http://schemas.openxmlformats.org/wordprocessingml/2006/main}tr").Count();
              
                int no_of_cells = xml.Root.Descendants("{http://schemas.openxmlformats.org/wordprocessingml/2006/main}tc").Count();
    
                int no_of_columns = (no_of_cells/no_of_rows);
    
               string[,] array = new string[no_of_columns,no_of_rows];
   
               int w = 0;
               int k = 0;
   
   
               var cells = xml.Root.Descendants("{http://schemas.openxmlformats.org/wordprocessingml/2006/main}t");
   
               foreach (var cell in cells)
               {
                  
                   array[k, w] = cell.Value;
   
                   k++;
                   if (k == (no_of_columns))
                   {
                       k = 0;
   
                       w++;
   
                       if (w == (no_of_rows))
                       {
                      
                           break;
                       }
   
                   }
   
   
   
               }
   
    


它基本上读取xml文档(顺便说一句xml)并用数据填充数组。

It basically reads xml document (word xml by the way) and fills the array with data.

它在.net 3.5项目上工作正常,但是当我将此代码应用到先前从.net 2.0转换的.net 3.5项目时,不会编译。

It works fine on the .net 3.5 project, but does not compile when I apply this code into a .net 3.5 peoject previously converted from .net 2.0.

它产生以下错误:


错误1'System.Collections.Generic.IEnumerable< System.Xml.Linq.XElement>'不包含'Count'的定义
错误2'System.Collections.Generic.IEnumerable< System .Xml.Linq.XElement>'不包含'Count'的定义
错误3无法找到类型或命名空间名称'var'(您是否缺少using指令或程序集引用?)


Error    1    'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' does not contain a definition for 'Count'       
Error    2    'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' does not contain a definition for 'Count'       
Error    3    The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)       
Error    4    foreach statement cannot operate on variables of type 'var' because 'var' does not contain a public definition for 'GetEnumerator'       
Error    5    The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

当然我添加了System.Core,System.Xml.Linq引用和using指令(使用System.Linq,使用System.Xml.Linq)。

Of course I added System.Core , System.Xml.Linq references and the using directives (using System.Linq , using System.Xml.Linq).

推荐答案

对于缺少的.Count,您需要添加对System.Core程序集的引用。这就是IEnumerable< T>的新方法。实施。我不确定另一个问题,但希望添加引用将解决这两个问题

Pawel

For the missing .Count you need to add a reference to System.Core assembly. This is where new methods on IEnumerable<T> are implemented. I am not sure about the other issue but hopefully adding the reference will solve both problems

Pawel


这篇关于.net 3.5项目中的Linq to XML问题(以前是从.net 2.0转换而来的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆