2

我很困惑......我再也没有什么想法了......我需要从这种类型中读取每个属性xmldocument

<party_list>
   <party currency="BAM" id="330-159210014235851" />
   <party currency="EUR" id="330-153990008540078" />
</party_list>

我有这个......但无济于事

XDocument doc = XDocument.Load(@"e:\input\partylist.xml");
XElement root1 = doc.Element("party_list");
foreach (XElement element in root1.Elements())
{
   MessageBox.Show(element.Element("party").Value);
}
4

7 回答 7

2

代替

MessageBox.Show(element.Element("party").Value);

MessageBox.Show((string)element.Attribute["currency"]);

因为您的元素已经是party元素。


完整代码:

XDocument doc = XDocument.Load(@"e:\input\partylist.xml");
foreach (var party in doc.Descendants("party"))
   foreach (var attr in party.Attributes())
        MessageBox.Show(attr.ToString());

此代码使用重写ToString()的方法,XAttribute该方法返回属性名称及其值,如下所示:

currency="BAM"

如果需要,您可以仅使用值:MessageBox.Show(attr.Value);

于 2013-04-16T06:49:59.417 回答
0
XmlNodeList elemList = doc.GetElementsByTagName(...);
    for (int i = 0; i < elemList.Count; i++)
    {
        string attrVal = elemList[i].Attributes["id"].Value;
    }  

试试上面的代码。

于 2013-04-16T06:48:44.883 回答
0

你可以试试这个吗?

XDocument doc = XDocument.Load(@"e:\input\partylist.xml");
foreach (var partyList in doc.Descendants("party"))
{
   string currency= partyList .Attribute("currency").Value;
   string id= partyList .Attribute("id").Value;
}
于 2013-04-16T06:53:10.057 回答
0

输入xml文件

<?xml version='1.0' encoding='utf-16'?>
<party_list>
  <party currency='BAM' id='330-159210014235851' />
  <party currency='EUR' id='330-153990008540078' />
</party_list>

获取属性值的 C# 代码

        XDocument doc = XDocument.Load(@"e:\input\partylist.xml");
        var eles = doc.Element("party_list").Elements("party");

        foreach (XElement ele in eles)
        {
            Console.WriteLine (ele.Attribute("currency").Value);
            Console.WriteLine(ele.Attribute("id").Value);

        }
        Console.ReadLine();
于 2013-04-16T06:58:24.800 回答
0
string body = @"<?xml version='1.0' encoding='utf-16'?><party_list>
                    <party currency='BAM' id='330-159210014235851' />
                    <party currency='EUR' id='330-153990008540078' />
                </party_list>";
XmlDocument bodyDoc = new XmlDocument();
bodyDoc.LoadXml(body);
XmlNodeList xnList = bodyDoc.SelectNodes("/party_list/party");
foreach (XmlNode item in xnList)
{
    var id =  item.Attributes["id"].Value;
    var currency = item.Attributes["currency"].Value;
}
于 2013-04-16T07:04:08.267 回答
0

另外的选择:

XElement xml = 
  XElement.Parse (@"<party_list>
                      <party currency='BAM' id='330-159210014235851' /> 
                      <party currency='EUR' id='330-153990008540078' />
                    </party_list>");

xml.Elements()
     .SelectMany(element => element.Attributes().Select(attrib => attrib.Value))
       .ToList()
         .ForEach(text => MessageBox.Show(text));
于 2013-04-16T07:09:27.050 回答
0

尝试这个

XDocument doc = XDpcument.Load("your xml location");

var parties = from p in doc.Descendants("party")
              select new 
              { 
                      currency = p.Attribute("currency").Value;
                      Id =       p. Attribute("id").Value;
              }
于 2013-04-16T08:10:41.533 回答