使用C#和LINQ搜索EDMX元数据无结果

问题描述

我正在尝试解析并通过此元数据搜索我正在C#中从事的项目。 最终结果是加载xml文件(如下),找到名称为“ docChemicalReport”的EntityType,然后循环遍历表结构。简单的目标。我遇到的问题是我无法让它返回任何东西。

元数据在这里https://apps.fielddirect.com/DataServices/OData/$metadata 示例代码

<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:https://docs.microsoft.com/en-us/dotnet/standard/linq/find-element-specific-attribute

因此,我从根目录中获取名称空间,并进行了快速查询,但没有任何结果。

    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; }
    }

}