开发者

Load selective XML data into Dataset using C#

开发者 https://www.devze.com 2023-03-18 06:26 出处:网络
Appreciate some pointers on how can I use XPath on XML file to extract only some data and load it into a dataset.

Appreciate some pointers on how can I use XPath on XML file to extract only some data and load it into a dataset.

ds.ReadXml(fsReadXml);

will load the entire xml into dataset but my requirement is to load only particular node and values to dataset.

Sample xml data:

<data cobdate="5 Jul 2011" DBStatus="">
  <view>BOTH</view>
  <show_acctnbr>true</show_acctnbr>
  <summary>
    <headings sum="Summary" real_per="Realized this period" real_trd="Profit/l开发者_如何学JAVAoss in trading currency" real_select="Profit/loss in selected currency" short_term="Short Term Profit/Loss" long_term="Long Term Profit/Loss" />
    <account number="A123456" curr_code="USD" curr_desc="US Dollars" tradecurrvalue="123,123.00" selectcurrvalue="123,123.00" managed="NO" />
    <account number="P123456" curr_code="USD" curr_desc="US Dollars" tradecurrvalue="0.00" selectcurrvalue="0.00" managed="NO" />
  </summary>
  <detail>
    <headings dateaq="Date acquired" datesld="Date sold" desc="Description" sec_nbr="Security number " qty="Quantity" cost="Cost basis" />
    <account number="A123456" currency="US Dollars">
      <item datesold="29 Apr 11" sec_nbr="1234" description="SOME VALUE(USD)" quantity="8,000" proceeds="123,123.0" />
      <item datesold="29 Apr 11" sec_nbr="4567" description="SOME VALUE(USD)" quantity="9,000" proceeds="123,123.0" />
    </account>
    <account number="P123456" currency="US Dollars">
      <item datesold="29 Apr 11" sec_nbr="1234" description="SOME VALUE(USD)" quantity="8,000" proceeds="123,123.0" />
      <item datesold="29 Apr 11" sec_nbr="4567" description="SOME VALUE(USD)" quantity="9,000" proceeds="123,123.00" />
    </account>
  </detail>
</data>

In this example data, I just need to load accounts from <summary> node and if possible only number, tradecurrvalue and selectcurrvalue attributes. I using C# and 3.5.


With XDocument:

var doc = XDocument.Load(fileName);
var lst = doc
       .Descendants("summary")   // don't care where summary is
       .Elements("account ")     // direct child of <summary>
       .Select(x => new 
       {
          number = x.Attribute("number").Value,
          ...
       });

foreach(var account in lst) 
{
  .... // add to DataSet
}


can also use XmlNodeList and XmlNode, light weight compared to others

  foreach (XmlNode node in nodes)
            {
                dt.Rows.Add(node["Name"].InnerText.ToString(),
                            node["NoOfDay"].InnerText.ToString(),
                            node["dateColumn"].InnerText.ToString()
                           );
            }

Reference Link


Evaluate this XPath expression in your code:

/*/summary/account/@*
            [contains('|number|tradecurrvalue|selectcurrvalue|',
                      concat('|',name(),'|')
                      )
            ]

This selects any attribute (of any account element that is a child of any summary element that is a child of the top element in the XML document), named "number", "tradecurrvalue" or "selectcurrvalue"

It is very easy to enlarge the list of possible attribute names you want selected -- just include them in the pipe-delimited name list.

0

精彩评论

暂无评论...
验证码 换一张
取 消