1

我在尝试对两个单独的 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 设置为如果较新

这是异常,并且 var 值为 null,因此由于某种原因它肯定不会加载: 异常截图

已解决:我在这里学到的教训是,您需要密切关注您的 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
                    };
4

1 回答 1

1

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

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

请注意,LoadXMLOrders()可以为每个客户再次调用此类查询。所以先预存一下。

也不要忘记具体化调用ToArray()/的查询ToList()

于 2012-10-19T02:39:16.867 回答