0

我正在使用来自http://odata.github.io的 ODataLib来生成 OData 有效负载(使用本教程http://odata.github.io/odata.net/#01-01-write-payload

它工作正常......几乎。事实上我需要

<feed xml:base="http://bdxweb014:8091/MongoData.svc/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
<id>test</id>
<title type="text">logAGG</title>
<updated>2015-06-30T10:06:54Z</updated>
<entry>
    <id>logAGG('543b76c2d83416a042cb34c3')</id>
    <content type="application/xml">
        <m:properties>
            <d:Id>543b76c2d83416a042cb34c3</d:Id>
            <d:Date m:type="Edm.DateTime">2014-10-12T00:00:00+02:00</d:Date>
            <d:hitCacheRatio m:type="Edm.Double">0.6908422811838677</d:hitCacheRatio>
            <d:nbCalls m:type="Edm.Int64">520242</d:nbCalls>
            <d:avgResponseTime m:type="Edm.Double">639.71664340826</d:avgResponseTime>
            <d:evoHourD1 m:type="Collection(WebApplication2.EvoHourD1)">
                <d:element>
                    <d:dateTime m:type="Edm.DateTime">2014-10-12T22:00:00+02:00</d:dateTime>
                    <d:total m:type="Edm.Int64">19884</d:total>
                </d:element>
                <d:element>
                    <d:dateTime m:type="Edm.DateTime">2014-10-12T21:00:00+02:00</d:dateTime>
                    <d:total m:type="Edm.Int64">22882</d:total>
                </d:element>
...

如您所见,“properties”标签包含许多原始类型属性和一个集合类型属性(EvoHourD1)。

就我而言,我不能使用模型(因为一切都是动态的)。我不能使用 complexType,因为电源查询不支持它。

这是我用来生成提要的代码:

        //Get the data from another API
        var response = GetAsync("https://my.api.call.com/parameters");
        dynamic d = JObject.Parse(response.Result);

        MemoryStream stream = new MemoryStream();
        InMemoryMessage message = new InMemoryMessage() { Stream = stream };

        ODataMessageWriterSettings settings = new ODataMessageWriterSettings();
        settings.Indent = true;
        settings.ODataUri = new ODataUri()
        {
            ServiceRoot = new Uri("http://services.odata.org/V4/OData/OData.svc/")
        };

        ODataMessageWriter writer = new ODataMessageWriter((IODataResponseMessage)message, settings);

        ODataWriter odataWriter = writer.CreateODataFeedWriter();

        ODataFeed feed = new ODataFeed();

        feed.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo()
        {
            NavigationSourceName = "Customers",
            NavigationSourceEntityTypeName = "Customer"
        });

        odataWriter.WriteStart(feed);

        foreach (var row in d.DataFeed[0].Rows)
        {
            ODataEntry entry = new ODataEntry();
            ODataProperty toto = new ODataProperty();
            List<ODataProperty> props = new List<ODataProperty>();

            foreach (var col in d.DataFeed[0].Columns)
            {
                props.Add(new ODataProperty() { Name = col.Name.ToString(), Value = row[col.Name.ToString()].ToString() });
            }
            entry.Properties = props.ToArray<ODataProperty>();
            odataWriter.WriteStart(entry);
            odataWriter.WriteEnd();
        }


        odataWriter.WriteEnd();

        string output = Encoding.UTF8.GetString(stream.ToArray());
        Console.WriteLine(output);
        Console.ReadKey();

谢谢您的帮助。

4

0 回答 0