Dregalia
Dregalia

Reputation: 61

Searching EDMX metadata using C# and LINQ Yields no results

I'm trying to parse out and search thru this metadata for an project I'm working on in C#. The end result, is to load the xml file (below), find the EntityType with the name "docChemicalReport" and then loop thru the table structure. Easy goal. The problem I'm having is I can't get it to return anything.

The Metadata is here: https://apps.fielddirect.com/DataServices/OData/$metadata Sample Code:

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

The example I'm using is from MS: https://learn.microsoft.com/en-us/dotnet/standard/linq/find-element-specific-attribute

So, I grab the name space from root, throw together a quick query, but it yields no results.

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

My question is, am I over complicating this? Should I use a different xml technology to search and read the properties? What part of my code is incorrect...

Upvotes: 0

Views: 238

Answers (1)

jdweng
jdweng

Reputation: 34421

See if following helps :

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

}

Upvotes: 1

Related Questions