如何将Java List XML导入c#

本文关键字:XML 导入 List Java | 更新日期: 2023-09-27 18:11:50

我有一个Java生成的(List Collection) XML格式:

<java.util.Collections>
   <org.yccheok.jstock.engine.Stock> 
     <code> 
       <code> RBS.L</code> 
     </code> 
     <symbol> 
       <symbol> ROYAL BK SCOTL GR</symbol> 
     </symbol> 
     <name> ROYAL BK SCOTL GR</name> 
     <board> London</board> 
     <industry> Unknown</industry> 
     <prevPrice> 23.74</prevPrice> 
     <openPrice> 23.41</openPrice> 
     <lastPrice> 24.4</lastPrice> 
     <highPrice> 24.855</highPrice> 
     <lowPrice> 23.0</lowPrice> 
     <volume> 51353968</volume> 
     <changePrice> 0.66</changePrice> 
     <changePricePercentage> 2.78</changePricePercentage> 
     <lastVolume> 795</lastVolume> 
     <buyPrice> 24.39</buyPrice> 
     <buyQuantity> 51203</buyQuantity> 
     <sellPrice> 24.4</sellPrice> 
     <sellQuantity> 370763</sellQuantity> 
     <secondBuyPrice> 0.0</secondBuyPrice> 
     <secondBuyQuantity> 0</secondBuyQuantity> 
     <secondSellPrice> 0.0</secondSellPrice> 
     <secondSellQuantity> 0</secondSellQuantity> 
     <thirdBuyPrice> 0.0</thirdBuyPrice> 
     <thirdBuyQuantity> 0</thirdBuyQuantity> 
     <thirdSellPrice> 0.0</thirdSellPrice> 
     <thirdSellQuantity> 0</thirdSellQuantity> 
     <calendar> 
       <time> 1319038099446</time> 
       <timezone> America/New_York</timezone> 
     </calendar> 
   </org.yccheok.jstock.engine.Stock>
</java.util.Collections>

我试图提取代码内部标签和c#中的changePricePercentage的标签值。我还试图用这些值预填充一个数据表。如何处理代码的内部标签?尽管我不是专家,这里是我的c#源代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data;
namespace XMLParser
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable table = new DataTable();
            table.Columns.Add("code", typeof(string)); ;
            table.Columns.Add("changePricePercentage", typeof(double));
            // Create a new XmlDocument  
            XmlDocument doc = new XmlDocument();
            // Load data  
            doc.Load(@"C:'...'realtimestock.xml");
            // Set up namespace manager for XPath  
            // Get forecast with XPath  
            //XmlNodeList nodes = doc.SelectNodes("org.yccheok.jstock.engine.Stock", ns);
            XmlNodeList nodes = doc.SelectNodes("org.yccheok.jstock.engine.Stock");
            // You can also get elements based on their tag name and namespace,  
            // though this isn't recommended  
            //XmlNodeList nodes = doc.GetElementsByTagName("org.yccheok.jstock.engine.Stock");
            //                          "http://xml.weather.yahoo.com/ns/rss/1.0");  
            foreach (XmlNode node in nodes)
            {
                // Console.WriteLine("{0}: {1}, {2}F - {3}F",
                //                     node.Attributes["code"].InnerText,
                //                     node.Attributes["changePricePercentage"].InnerText);
                Console.WriteLine("1: {0} 2: {1}", node.Attributes["code"].InnerText,
node.Attributes["changePricePercentage"].InnerText);
                table.Rows.Add(node.Attributes["code"].InnerText, node.Attributes["changePricePercentage"].InnerText);
                Console.ReadKey();
            }
        }
    }
}

我如何让我的代码完成这个任务?注:这个Stackoverflow编辑器不能正确地接受我的XML代码,所以我不得不使用实际的符号名称进行编辑。对不起由于

如何将Java List XML导入c#

试试这个:

XmlNodeList nodes = doc.SelectNodes("//org.yccheok.jstock.engine.Stock");
foreach (XmlElement element in nodes)
{
    Console.WriteLine("1: {0} 2: {1}", 
        element.SelectSingleNode("code").InnerText,
        element.SelectSingleNode("changePricePercentage").InnerText);
}
Console.ReadKey();

您的codechangePricePercentage节点是元素,而不是属性,这是您的错误。