如何将一个子节点的两个XML属性保存为List<>中的一个项?

huangapple go评论71阅读模式
英文:

How to save two XML attributes of one childnode as one item in a List<>

问题

I want to save two XML attributes of a child node combined in a List<> but they are saved individually and not somehow merged within the result.Add(). I would like to have the List with 30 entries, each entry should have currency and rate as items. Now there are 61 entries, one for each item. I want to convert that later to a JSON to pass on to a Service which stores that two values into a database.

Screenshot of the List<> result

The XML I'm working with is https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
The Childnodes are and the Attributes are "currency" and "rate"

( I dont know how to do this with Linq expressions but there surely is a better way)

// This Method iterates through every Node if it has Attributes:

static void ecbXmlNanny(XmlNode ecbNode, List<ecbItems> result)
{
 // if Node has ChildNodes
 if (ecbNode != null && ecbNode.HasChildNodes)
 {
  foreach (XmlNode node in ecbNode.ChildNodes)
  {
   if (node.Attributes != null)
   {                       
    foreach (XmlAttribute attribute in node.Attributes)
    {
     listItems item = new listItems();

     // the needed Attributes are "currency" and "rate" 
     if (attribute.Name == "currency")
      {
       item.ecbCurrency = attribute.Value;
      }
      if (attribute.Name == "rate")
      {
       item.ecbRate = Decimal.Parse(attribute.Value);
      }
      result.Add(new listItems() { ecbCurrency = item.ecbCurrency, ecbRate = item.ecbRate });
     }
    }
    ecbXmlNanny(node, result);
   }
  }
}
英文:

I want to save two XML attributes of a child node combined in a List<> but they are saved individually and not somehow merged within the result.Add(). I would like to have the List with 30 entries, each entry should have currency and rate as items. Now there are 61 entries, one for each item. I want to convert that later to a JSON to pass on to a Service which stores that two values into a database.

Screenshot of the List<>result

The XML I'm working with is https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
The Childnodes are <CUBE> and the Attributes are "currency" and "rate"

( I dont know how to do this with Linq expressions but there surely is a better way)

// This Method iterates through every Node if it has Attributes:

static void ecbXmlNanny(XmlNode ecbNode, List&lt;ecbItems&gt; result)
{
 // if Node has ChildNodes
 if (ecbNode != null &amp;&amp; ecbNode.HasChildNodes)
 {
  foreach (XmlNode node in ecbNode.ChildNodes)
  {
   if (node.Attributes != null)
   {                       
    foreach (XmlAttribute attribute in node.Attributes)
    {
     listItems item = new listItems();

     // the needed Attributes are &quot;currency&quot; and &quot;rate&quot; 
     if (attribute.Name == &quot;currency&quot;)
      {
       item.ecbCurrency = attribute.Value;
      }
      if (attribute.Name == &quot;rate&quot;)
      {
       item.ecbRate = Decimal.Parse(attribute.Value);
      }
      result.Add(new listItems() { ecbCurrency = item.ecbCurrency, ecbRate = item.ecbRate });
     }
    }
    ecbXmlNanny(node, result);
   }
  }
}

答案1

得分: 1

这段代码对我有效:

XDocument doc = XDocument.Parse(xml);
var results = doc.Elements()
    .Descendants()
    .Where(e => e.Attribute("currency") != null)
    .Select(e => new
    {
        currency = e.Attribute("currency").Value,
        rate = e.Attribute("rate").Value
    })
    .ToList();

结果(以 JSON 格式):

[
  {
    "currency": "USD",
    "rate": "1.0697"
  },
  {
    "currency": "JPY",
    "rate": "149.25"
  },
  {
    "currency": "BGN",
    "rate": "1.9558"
  },
  {
    "currency": "CZK",
    "rate": "23.687"
  },
  {
    "currency": "DKK",
    "rate": "7.4477"
  },
  {
    "currency": "GBP",
    "rate": "0.85823"
  },
  {
    "currency": "HUF",
    "rate": "370.42"
  },
  {
    "currency": "PLN",
    "rate": "4.5410"
  },
  {
    "currency": "RON",
    "rate": "4.9645"
  },
  {
    "currency": "SEK",
    "rate": "11.6455"
  },
  {
    "currency": "CHF",
    "rate": "0.9732"
  },
  {
    "currency": "ISK",
    "rate": "149.70"
  },
  {
    "currency": "NOK",
    "rate": "11.9670"
  },
  {
    "currency": "TRY",
    "rate": "22.2520"
  },
  {
    "currency": "AUD",
    "rate": "1.6407"
  },
  {
    "currency": "BRL",
    "rate": "5.4032"
  },
  {
    "currency": "CAD",
    "rate": "1.4514"
  },
  {
    "currency": "CNY",
    "rate": "7.6066"
  },
  {
    "currency": "HKD",
    "rate": "8.3786"
  },
  {
    "currency": "IDR",
    "rate": "15964.76"
  },
  {
    "currency": "ILS",
    "rate": "3.9970"
  },
  {
    "currency": "INR",
    "rate": "88.2070"
  },
  {
    "currency": "KRW",
    "rate": "1413.23"
  },
  {
    "currency": "MXN",
    "rate": "18.8695"
  },
  {
    "currency": "MYR",
    "rate": "4.9351"
  },
  {
    "currency": "NZD",
    "rate": "1.7775"
  },
  {
    "currency": "PHP",
    "rate": "60.057"
  },
  {
    "currency": "SGD",
    "rate": "1.4466"
  },
  {
    "currency": "THB",
    "rate": "37.204"
  },
  {
    "currency": "ZAR",
    "rate": "21.1567"
  }
]

请注意,我已经将代码部分保持不变,只翻译了注释和结果部分。

英文:

this code works for me

XDocument doc = XDocument.Parse(xml);
var results = doc.Elements()
.Descendants()
.Where(e =&gt; e.Attribute(&quot;currency&quot;) != null)
.Select(e =&gt; new
{
currency = e.Attribute(&quot;currency&quot;).Value,
rate = e.Attribute(&quot;rate&quot;).Value
})
.ToList();

result (in json format)

[
{
&quot;currency&quot;: &quot;USD&quot;,
&quot;rate&quot;: &quot;1.0697&quot;
},
{
&quot;currency&quot;: &quot;JPY&quot;,
&quot;rate&quot;: &quot;149.25&quot;
},
{
&quot;currency&quot;: &quot;BGN&quot;,
&quot;rate&quot;: &quot;1.9558&quot;
},
{
&quot;currency&quot;: &quot;CZK&quot;,
&quot;rate&quot;: &quot;23.687&quot;
},
{
&quot;currency&quot;: &quot;DKK&quot;,
&quot;rate&quot;: &quot;7.4477&quot;
},
{
&quot;currency&quot;: &quot;GBP&quot;,
&quot;rate&quot;: &quot;0.85823&quot;
},
{
&quot;currency&quot;: &quot;HUF&quot;,
&quot;rate&quot;: &quot;370.42&quot;
},
{
&quot;currency&quot;: &quot;PLN&quot;,
&quot;rate&quot;: &quot;4.5410&quot;
},
{
&quot;currency&quot;: &quot;RON&quot;,
&quot;rate&quot;: &quot;4.9645&quot;
},
{
&quot;currency&quot;: &quot;SEK&quot;,
&quot;rate&quot;: &quot;11.6455&quot;
},
{
&quot;currency&quot;: &quot;CHF&quot;,
&quot;rate&quot;: &quot;0.9732&quot;
},
{
&quot;currency&quot;: &quot;ISK&quot;,
&quot;rate&quot;: &quot;149.70&quot;
},
{
&quot;currency&quot;: &quot;NOK&quot;,
&quot;rate&quot;: &quot;11.9670&quot;
},
{
&quot;currency&quot;: &quot;TRY&quot;,
&quot;rate&quot;: &quot;22.2520&quot;
},
{
&quot;currency&quot;: &quot;AUD&quot;,
&quot;rate&quot;: &quot;1.6407&quot;
},
{
&quot;currency&quot;: &quot;BRL&quot;,
&quot;rate&quot;: &quot;5.4032&quot;
},
{
&quot;currency&quot;: &quot;CAD&quot;,
&quot;rate&quot;: &quot;1.4514&quot;
},
{
&quot;currency&quot;: &quot;CNY&quot;,
&quot;rate&quot;: &quot;7.6066&quot;
},
{
&quot;currency&quot;: &quot;HKD&quot;,
&quot;rate&quot;: &quot;8.3786&quot;
},
{
&quot;currency&quot;: &quot;IDR&quot;,
&quot;rate&quot;: &quot;15964.76&quot;
},
{
&quot;currency&quot;: &quot;ILS&quot;,
&quot;rate&quot;: &quot;3.9970&quot;
},
{
&quot;currency&quot;: &quot;INR&quot;,
&quot;rate&quot;: &quot;88.2070&quot;
},
{
&quot;currency&quot;: &quot;KRW&quot;,
&quot;rate&quot;: &quot;1413.23&quot;
},
{
&quot;currency&quot;: &quot;MXN&quot;,
&quot;rate&quot;: &quot;18.8695&quot;
},
{
&quot;currency&quot;: &quot;MYR&quot;,
&quot;rate&quot;: &quot;4.9351&quot;
},
{
&quot;currency&quot;: &quot;NZD&quot;,
&quot;rate&quot;: &quot;1.7775&quot;
},
{
&quot;currency&quot;: &quot;PHP&quot;,
&quot;rate&quot;: &quot;60.057&quot;
},
{
&quot;currency&quot;: &quot;SGD&quot;,
&quot;rate&quot;: &quot;1.4466&quot;
},
{
&quot;currency&quot;: &quot;THB&quot;,
&quot;rate&quot;: &quot;37.204&quot;
},
{
&quot;currency&quot;: &quot;ZAR&quot;,
&quot;rate&quot;: &quot;21.1567&quot;
}
]

答案2

得分: -1

这解决了我的问题,我将结果放在了内部循环外面,并从列表中删除了所有空值或 o 项:

            static void ecbXmlNanny(XmlNode ecbNode, List<ecbItems> result)
        {
            ecbItems item = new ecbItems();

            // if Node has ChildNodes
            if (ecbNode != null && ecbNode.HasChildNodes)
            {
                foreach (XmlNode node in ecbNode.ChildNodes)
                {
                    // if ChildNode != empty
                    if (node.Attributes != null)
                    {
                        foreach (XmlAttribute attribute in node.Attributes)
                        {
                            // the needed Attributes are "currency" and "rate" 
                            if (attribute.Name == "currency")
                            {
                                item.ecbCurrency = attribute.Value;
                            }
                            if (attribute.Name == "rate")
                            {
                                item.ecbRate = Decimal.Parse(attribute.Value);
                            }
                            
                        }
                        result.Add(new ecbItems() { ecbCurrency = item.ecbCurrency, ecbRate = item.ecbRate });
                        
                    }
                    
                    ecbXmlNanny(node, result);
                }
            }
            result.RemoveAll(item => item.ecbCurrency == null && item.ecbRate == 0);
        }
英文:

this solved my issue, i put the result outside the inner Loop and removed all null or o items from the List:

        static void ecbXmlNanny(XmlNode ecbNode, List&lt;ecbItems&gt; result)
{
ecbItems item = new ecbItems();
// if Node has ChildNodes
if (ecbNode != null &amp;&amp; ecbNode.HasChildNodes)
{
foreach (XmlNode node in ecbNode.ChildNodes)
{
// if ChildNode != empty
if (node.Attributes != null)
{
foreach (XmlAttribute attribute in node.Attributes)
{
// the needed Attributes are &quot;currency&quot; and &quot;rate&quot; 
if (attribute.Name == &quot;currency&quot;)
{
item.ecbCurrency = attribute.Value;
}
if (attribute.Name == &quot;rate&quot;)
{
item.ecbRate = Decimal.Parse(attribute.Value);
}
}
result.Add(new ecbItems() { ecbCurrency = item.ecbCurrency, ecbRate = item.ecbRate });
}
ecbXmlNanny(node, result);
}
}
result.RemoveAll(item =&gt; item.ecbCurrency == null &amp;&amp; item.ecbRate == 0);
}

huangapple
  • 本文由 发表于 2023年6月1日 17:05:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380296.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定