我需要动态选择所有元素名称,例如货币名称EURUSD,EURGBP ......:
<broker name="MBT Free EXN">
<EURUSD b="1.34565" a="1.34608" s="4.3"/>
<EURGBP b="0.85151" a="0.85227" s="7.6"/>
<EURAUD b="1.29048" a="1.29157" s="10.9"/>
<EURJPY b="122.229" a="122.281" s="5.2"/>
<EURCHF b="1.24661" a="1.24749" s="8.8"/>
<EURCAD b="1.35341" a="1.35429" s="8.8"/>
<USDJPY b="90.828" a="90.844" s="1.6"/>
<USDCHF b="0.92618" a="0.927" s="8.2"/>
<USDCAD b="1.0057" a="1.00609" s="3.9"/>
<GBPUSD b="1.57935" a="1.58059" s="12.4"/>
<GBPJPY b="143.461" a="143.569" s="10.8"/>
<GBPCHF b="1.46327" a="1.46464" s="13.7"/>
<GBPAUD b="1.51486" a="1.51633" s="14.7"/>
<GBPCAD b="1.58829" a="1.5903" s="20.1"/>
<AUDUSD b="1.04202" a="1.04298" s="9.6"/>
<AUDJPY b="94.659" a="94.738" s="7.9"/>
<AUDCAD b="1.04799" a="1.04918" s="11.9"/>
<AUDNZD b="1.24304" a="1.24592" s="28.8"/>
<AUDCHF b="0.96537" a="0.96653" s="11.6"/>
<NZDUSD b="0.83728" a="0.83833" s="10.5"/>
<NZDJPY b="76.052" a="76.142" s="9"/>
<CADJPY b="90.274" a="90.327" s="5.3"/>
<CHFJPY b="97.985" a="98.087" s="10.2"/>
</broker>
答案 0 :(得分:2)
怎么样:
var names = doc.Root.Elements().Select(e => e.Name.LocalName);
doc
将初始化为XDocument
。
答案 1 :(得分:0)
Tyr this:
XElement element = XElement.Parse(xml); // if you have xml as string or
// XElement element = XElement.Load(File.Open(path, FileMode.Open)) if xml in file
var nameList = element.Elements().Select(e=>e.Name.LocalName).ToList();