如何在C#中将Odata $ Metadata XML deseril对象

时间:2015-06-26 12:28:44

标签: xml c#-4.0 odata metadata

我想在c#类对象中反序列化OData $ metadata(edmx)xml

元数据xml

    <edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
      <edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0">
        <Schema Namespace="LetsGo.Models" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
          <EntityType Name="Customer">               
            <Property Name="CustomerID" Type="Edm.Int32" Nullable="false" />
            <Property Name="FirstName" Type="Edm.String" Nullable="true" MaxLength="Max" Unicode="true" FixedLength="false" />
            <Property Name="MiddleName" Type="Edm.String" Nullable="true" MaxLength="Max" Unicode="true" FixedLength="false" />
    </EntityType>
 <EntityType Name="Customer1">                
            <Property Name="CustomerID" Type="Edm.Int32" Nullable="false" />
            <Property Name="FirstName" Type="Edm.String" Nullable="true" MaxLength="Max" Unicode="true" FixedLength="false" />
            <Property Name="MiddleName" Type="Edm.String" Nullable="true" MaxLength="Max" Unicode="true" FixedLength="false" />
    </EntityType>
     </Schema>
      </edmx:DataServices>
    </edmx:Edmx>

这是我的课。我手动创建了它。

   [Serializable, XmlRoot(ElementName = "Edmx", Namespace = "")]
    public class RootModel
    {
        [XmlElement(ElementName = "DataServices", Namespace = "")]
        public DataServices DataService { get; set; }
    }

    [Serializable, XmlRoot(ElementName = "DataServices", Namespace = "")]
    public class DataServices
    {
        [XmlElement(ElementName = "Schema", Namespace = "LetsGo.Models")]
        public ModelsSchema Models { get; set; }
    }

    [Serializable, XmlRoot(ElementName = "Schema", Namespace = "LetsGo.Models")]
    public class ModelsSchema
    {
        [XmlElement(ElementName = "EntityType", Namespace = "")]
        public EntityType[] Entities { get; set; }
    }

    [Serializable, XmlRoot(ElementName = "EntityType", Namespace = "")]
    public class EntityType
    {
        public string Name { get; set; }

        [XmlElement(ElementName = "Property", Namespace = "")]
        public Property[] Properties { get; set; }
    }

    [Serializable, XmlRoot(ElementName = "Property", Namespace = "")]
    public class Property
    {
        public string Name { get; set; }
        public string Type { get; set; }
        public bool Nullable { get; set; }
    }

反序列化代码

RootModel OdataSchema = new RootModel();
Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(myxml));
System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(RootModel));
OdataSchema = (T)reader.Deserialize(stream);

这是一个错误:

  

发生了'System.InvalidOperationException'类型的异常   System.Xml.dll但未在用户代码中处理

     

其他信息:XML文档中存在错误(2,2)。

1 个答案:

答案 0 :(得分:0)

XML中的标记名称与您的c#不匹配。还有你的xml中的警告修复

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Class1.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
</Root>​

C#代码

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

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

            string myxml = File.ReadAllText(FILENAME);

            RootModel OdataSchema = new RootModel();
            Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(myxml));
            XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(RootModel));
            OdataSchema = (RootModel)reader.Deserialize(stream);
        }
    }

    [Serializable, XmlRoot(ElementName = "Root", Namespace = "")]
    public class RootModel
    {
        [XmlElement(ElementName = "ItemGroup", Namespace = "")]
        public List<DataServices> dataService { get; set; }
    }

    [Serializable, XmlRoot(ElementName = "ItemGroup", Namespace = "")]
    public class DataServices
    {
        [XmlElement(ElementName = "Reference", Namespace = "")]
        public List<Models> models { get; set; }
    }

    [Serializable, XmlRoot(ElementName = "Reference", Namespace = "")]
    public class Models
    {
        [XmlAttribute("Include")]
        public string Entities { get; set; }
    }

}