使用 C# 和 LINQ 搜索 EDMX 元数据没有结果
Searching EDMX metadata using C# and LINQ Yields no results
我正在尝试通过此元数据解析并搜索我正在使用 C# 进行的项目。
最终结果是加载 xml 文件(如下),找到名称为“docChemicalReport”的 EntityType,然后遍历 table 结构。轻松的目标。我遇到的问题是我什么也做不到 return。
元数据在这里: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
因此,我从根目录中获取名称 space,拼凑了一个快速查询,但没有产生任何结果。
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; }
}
}
我正在尝试通过此元数据解析并搜索我正在使用 C# 进行的项目。 最终结果是加载 xml 文件(如下),找到名称为“docChemicalReport”的 EntityType,然后遍历 table 结构。轻松的目标。我遇到的问题是我什么也做不到 return。
元数据在这里: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
因此,我从根目录中获取名称 space,拼凑了一个快速查询,但没有产生任何结果。
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; }
}
}