LINQ to XML加入问题

时间:2012-10-19 02:31:33

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

我正在尝试在两个单独的XML文件上执行连接时出现问题。

我有两个类来描述我从XML文件中提取的每个对象,它们是

public class XMLCustomers
{
    public String CustomerID { get; set; }
    public String ContactName { get; set; }
}

public class XMLOrders
{
    public String OrderID { get; set; }
    public String CustomerID { get; set; }
    public String OrderDate { get; set; }
    public String ShippedDate { get; set; }
    public String Freight { get; set; }
    public String ShipName { get; set; }
    public String ShipCity { get; set; }
    public String ShipCountry { get; set; }
}

我的上一个类存储了我对两组数据执行的连接。

public class PostXMLJoinOrder
{
    public String OrderID { get; set;}
    public String ContactName { get; set;}
    public String OrderDate { get; set;}
    public String ShippedDate { get; set;}
    public String Freight { get; set;}
    public String ShipName { get; set;}
    public String ShipCity { get; set;}
    public String ShipCountry { get; set;}
}

最后,这是我从XML文件加载信息的两种方法,第三种方法执行连接并将信息存储在IEnumerable中

        public IEnumerable<XMLCustomers> LoadXMLCustomers() {
        var custs = from x in XElement.Load(System.Web.HttpContext.Current.Server.MapPath(@"~\App_Data\XCustomers.xml")).Elements()
                    select new XMLCustomers
                    {
                        ContactName = x.Attribute("ContactName").Value,
                        CustomerID = x.Attribute("CustomerID").Value

                    };
        int size = custs.Count();
        return custs;
    }

    public IEnumerable<XMLOrders> LoadXMLOrders() {
        var ords = from x in XElement.Load(System.Web.HttpContext.Current.Server.MapPath(@"~\App_Data\XOrders.xml")).Elements()
                    select new XMLOrders
                    {

                        OrderID = x.Attribute("ContactName").Value,
                        CustomerID = x.Attribute("CustomerID").Value,
                        OrderDate = x.Attribute("OrderDate").Value,
                        ShippedDate = x.Attribute("ShippedDate").Value,
                        Freight = x.Attribute("Freight").Value,
                        ShipName = x.Attribute("ShipName").Value,
                        ShipCity = x.Attribute("ShipCity").Value,
                        ShipCountry = x.Attribute("ShipCountry").Value
                    };
        int size = ords.Count();
        return ords;

    }


    public IEnumerable<PostXMLJoinOrder> LoadPostXMLJoinOrders() {
        var joinQuery = from customer in LoadXMLCustomers()
                        from orders in LoadXMLOrders()
                        where customer.CustomerID == orders.CustomerID
                        select new PostXMLJoinOrder
                        {

                            OrderID = orders.OrderID,
                            ContactName = customer.ContactName,
                            OrderDate = orders.OrderDate,
                            ShippedDate = orders.ShippedDate,
                            Freight = orders.Freight,
                            ShipName = orders.ShipName,
                            ShipCity = orders.ShipCity,
                            ShipCountry = orders.ShipCountry

                        };
        return joinQuery;


    }

我测试了从LINQ返回的项目数量,它仍然是0; 我已经仔细检查了从文件中加载的所有内容,但我似乎无法理解它出错的地方。

编辑: 它是一个装载问题。 XML文件肯定存储在正确的App_Data文件夹下。但是当单个LoadXMLCustomers()运行时,我在lINQ语句选择并创建新的Loaded Customer对象的部分获得NullReferenceException。

我已经确定XML文档的构建是内容,而copyToOutputDirectory设置为更新

这是例外&amp; var值也是null,因此由于某种原因它绝对不会加载: Exception Screenshot

解决:我在这里学到的教训是,您需要密切关注XML和数据。如果您的某些XML数据具有空值,则需要通过确保select语句可以处理它来进行帐户。

上面我有代码

                    select new XMLOrders
                {

                    OrderID = x.Attribute("ContactName").Value,
                    CustomerID = x.Attribute("CustomerID").Value,
                    OrderDate = x.Attribute("OrderDate").Value,
                    ShippedDate = x.Attribute("ShippedDate").Value,
                    Freight = x.Attribute("Freight").Value,
                    ShipName = x.Attribute("ShipName").Value,
                    ShipCity = x.Attribute("ShipCity").Value,
                    ShipCountry = x.Attribute("ShipCountry").Value
                };

哪个应该包含强制转换为字符串值,所以如果有空数据则“”不会抛出空异常。 其次,我应该得到的元素值不是属性值。

                    select new XMLOrders
                    {

                        OrderID = (string)x.Element("OrderID").Value,
                        CustomerID = (string)x.Element("CustomerID").Value,
                        OrderDate = (string)x.Element("OrderDate").Value,
                        ShippedDate = (string)x.Element("ShippedDate").Value,
                        Freight = (string)x.Element("Freight").Value,
                        ShipName = (string)x.Element("ShipName").Value,
                        ShipCity = (string)x.Element("ShipCity").Value,
                        ShipCountry = (string)x.Element("ShipCountry").Value
                    };

1 个答案:

答案 0 :(得分:1)

您是否尝试过不使用join,只是:

from customer in LoadXMLCustomers()
from order in LoadXMLOrders()
where customer.CustomerID = order.CustomerID
select new PostXMLJoinOrder
...

请注意,在此类查询中,可以再次为每个客户调用LoadXMLOrders()。所以先保存它。

另外,请不要忘记实现调用ToArray() / ToList()

的查询