读取xml属性

时间:2013-04-16 06:46:26

标签: c# xml linq linq-to-xml

我很困惑......我的脑子里再也没有了...我需要从这种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);
}

7 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

XmlNodeList elemList = doc.GetElementsByTagName(...);
    for (int i = 0; i < elemList.Count; i++)
    {
        string attrVal = elemList[i].Attributes["id"].Value;
    }  

尝试以上代码。

答案 2 :(得分: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;
}

答案 3 :(得分: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();

答案 4 :(得分: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;
}

答案 5 :(得分: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));

答案 6 :(得分: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;
              }