简体   繁体   中英

How to read specific fields from XML and load into Oracle db

my XML :

- <resources start="0" count="188">-
- <resource classname="Quote">
   <field name="name">USD/GEL</field> 
   <field name="price">2.418900</field> 
   <field name="symbol">GEL=X</field> 
   <field name="ts">1488758461</field> 
   <field name="type">currency</field> 
   <field name="utctime">2017-03-06T00:01:01+0000</field> 
   <field name="volume">0</field> 
   </resource>-  
   </resources>

C# Code:

 var xmlNodes = xElement.Descendants("resource")
            .Select(e => new
            {

                ConvertFrom = e.Attribute("symbol").Value,

                ConvRate = e.Attribute("price").Value,

                ConvDate = e.Attribute("utctime").Value
            });

I tried the above code to fetch and load into oracle but i got a below error.

System.Linq.Enumerable+WhereSelectEnumerableIterator 2[System.Xml.Linq.XElement,<>f__AnonymousType0 3[System.String,System.String,System.String]]

Please help me to resolve this issue.

I think you have misunderstood Attribute method, the name of the attribute is name , symbol is the value, so your query could be this way:

var xmlNodes = xElement.Descendants("resource")
                       .Select(e => new
                                    {

                                      ConvertFrom = (string)e.Elements().FistOrDefault(r=>r.Attribute("name").Value=="symbol"),
                                      ConvRate = (string)e.Elements().FistOrDefault(r=>r.Attribute("name").Value=="price"),
                                      ConvDate = (DateTime)e.Elements().FistOrDefault(r=>r.Attribute("name").Value=="utctime"),
                                     });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM