英文:
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
( 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<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);
}
}
}
答案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 => e.Attribute("currency") != null)
.Select(e => new
{
currency = e.Attribute("currency").Value,
rate = e.Attribute("rate").Value
})
.ToList();
result (in json format)
[
{
"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"
}
]
答案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<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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论