C# 使用C和LINQ搜索EDMX元数据不会产生任何结果

C# 使用C和LINQ搜索EDMX元数据不会产生任何结果,c#,xml,linq,edmx,C#,Xml,Linq,Edmx,我正试图通过这个元数据来解析和搜索我在C中工作的一个项目。 最终结果是加载下面的xml文件,找到名为docChemicalReport的EntityType,然后循环遍历表结构。轻松进球。我的问题是我不能让它归还任何东西 元数据如下所示: 示例代码: <edmx:Edmx Version="1.0"> <edmx:DataServices m:DataServiceVersion="3.0" m:MaxDataServiceVersion

我正试图通过这个元数据来解析和搜索我在C中工作的一个项目。 最终结果是加载下面的xml文件,找到名为docChemicalReport的EntityType,然后循环遍历表结构。轻松进球。我的问题是我不能让它归还任何东西

元数据如下所示: 示例代码:

<edmx:Edmx Version="1.0">
<edmx:DataServices m:DataServiceVersion="3.0" m:MaxDataServiceVersion="3.0">
<Schema Namespace="IHSFD.Database.Context">
<EntityType Name="CorpPurchaser">
<Key>
<PropertyRef Name="CorpPurchaserID"/>
</Key>
<Property Name="CorpPurchaserID" Type="Edm.Int32" Nullable="false"/>
<Property Name="CorpID" Type="Edm.Int32" Nullable="false"/>
<Property Name="CorpPurchaserName" Type="Edm.String" Nullable="false"/>
<Property Name="Email" Type="Edm.String"/>
<Property Name="PhoneNumber" Type="Edm.String"/>
<Property Name="PhoneExt" Type="Edm.String"/>
<Property Name="CreatedDate" Type="Edm.DateTime" Nullable="false"/>
<Property Name="CreatedBy" Type="Edm.String"/>
<Property Name="ModifiedDate" Type="Edm.DateTime" Nullable="false"/>
<Property Name="ModifiedBy" Type="Edm.String"/>
</EntityType>
<EntityType Name="docChemicalReport">
<Key>
<PropertyRef Name="DocIDChemicalReports"/>
</Key>
<Property Name="DocIDChemicalReports" Type="Edm.Int32" Nullable="false"/>
<Property Name="UserID" Type="Edm.String"/>
<Property Name="EntityID" Type="Edm.Int32" Nullable="false"/>
<Property Name="EntityTypeID" Type="Edm.Int16" Nullable="false"/>
<Property Name="docDate" Type="Edm.DateTime" Nullable="false"/>
<Property Name="ChemCoCode" Type="Edm.Int16"/>
<Property Name="ChemicalName" Type="Edm.String"/>
<Property Name="ChemTypeCode" Type="Edm.Int16"/>
<Property Name="InjectPointTypeID" Type="Edm.Int16"/>
<Property Name="BeginInven" Type="Edm.Single"/>
<Property Name="EndInven" Type="Edm.Single"/>
<Property Name="UnitsDelivered" Type="Edm.Single"/>
<Property Name="UnitsDelivCode" Type="Edm.Int16"/>
<Property Name="UnitsApplied" Type="Edm.Single"/>
<Property Name="UnitsAppliedCode" Type="Edm.Int16"/>
<Property Name="ApplicationCost" Type="Edm.Decimal"/>
<Property Name="AppMethodCode" Type="Edm.Int16"/>
<Property Name="UnitsFlush" Type="Edm.Single"/>
<Property Name="UnitsFlushCode" Type="Edm.Int16"/>
<Property Name="FlushTypeCode" Type="Edm.Int16"/>
<Property Name="Stamp" Type="Edm.DateTime" Nullable="false"/>
<Property Name="Notes" Type="Edm.String"/>
<Property Name="InputByID" Type="Edm.String"/>
<Property Name="DocSourceCode" Type="Edm.Int16"/>
</EntityType>
我使用的示例来自MS:

因此,我从根抓取名称空间,抛出一个快速查询,但它不会产生任何结果

    TextReader tr = new StringReader(responseFromServer);
    XDocument xmlDoc2 = XDocument.Load(tr);
   
    XElement root = xmlDoc2.Root;

    XElement entityType = root;
    XNamespace ns = entityType.GetDefaultNamespace();

    IEnumerable<XElement> et =
        from el in root.Elements(ns + "EntityType")
        where (string)el.Attribute(ns + "Name") == "docChemicalReport"
        select el;
    foreach (XElement el in et)
        Console.WriteLine(el);

我的问题是,我把事情复杂化了吗?我应该使用不同的xml技术来搜索和读取属性吗?我的代码哪部分不正确…

请查看以下帮助:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XElement schema = doc.Descendants().Where(x => x.Name.LocalName == "Schema").FirstOrDefault();
            int v = 0;
            string z = v.GetType().ToString(); 
            XNamespace ns = schema.GetDefaultNamespace();
            Dictionary<string, Entity> entityTypes = schema.Descendants(ns + "EntityType")
                .Select(x => new Entity() {
                    name = (string)x.Attribute("Name"),
                    key = (string)x.Descendants(ns + "PropertyRef").FirstOrDefault().Attribute("Name"),
                    properties = x.Elements(ns + "Property").Select(y => new Property()
                    {
                        name = (string)y.Attribute("Name"),
                        _type = Type.GetType("System." + ((string)y.Attribute("Type")).Split(new char[] {'.'}).Last()),
                        nullable = (y.Attribute("Nullable") == null)? (Boolean?)null : ((string)y.Attribute("Nullable") == "false")? false : true
                    }).ToList()
                }).GroupBy(x => x.key, y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
        }

    }
    public class Entity
    {
        public string name { get; set; }
        public string key { get; set; }
        public List<Property> properties { get; set; }
    }
    public class Property
    {
        public string name { get; set; }
        public Type _type { get; set; }
        public Boolean? nullable { get; set; }
    }

}

您有示例xml吗?属性上通常没有名称空间,并且标记名可能不在根下。因此,请尝试:从root.genderantsns+EntityType中的el开始,其中stringel.AttributeName==docChemicalReport@jdweng,我在文章的顶部发布了一个xml示例,以及一个指向实际xml的链接。将其更改为root。后代对tho没有帮助。仍然没有返回任何内容。对于任何可以玩的人,我将其加载到dotnetfiddle中。你可以通过阅读EF的元数据来做到这一点:@GertArnold,这看起来有点超出我的技能水平。我很感激你的评论,但我不明白我会如何在我的情况下使用它。我认为一个简单的linq查询将帮助我找到我需要去的地方。是的,所以我需要从中选择一个写着“docChemicalReport”的,然后循环遍历它的属性?这是我错过的部分,顺便说一句,谢谢你的耐心。因此,选择具有EntityType名称='docChemicalReport'的节点,然后循环遍历属性元素并将属性从中读取到列表中。感谢您的更新。让代码正常工作,我看到它被加载到一个字典中,里面有所有的信息,现在我只需要循环浏览字典,然后选择我假设的项目属性。我会搞砸这件事,但这比我自己能做的还要多。嘿@jdweng,所以,愚蠢的问题。。。我运行了代码,它工作了,但它删除了文件中的最后2个条目。。没有抱怨,因为我能够完成我的解决方案,只需手动处理其余的。。但当我查看创建的实体时,它们根本不在字典中。我很好奇为什么会发生这种情况,以备将来使用。字典可能不正常或是模式问题。哪些项目丢失了?出现警告消息:警告3命名空间中的元素“DataServices”在命名空间中具有无效的子元素“Schema”。可能元素的列表应为命名空间中的“Schema”。C:\Users\Joel\AppData\Local\Temporary Projects\ConsoleApplication1\XMLFile1.xml 4 6 ConsoleApplication1