Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有 XML 文件,我需要解析它并将数据导入数据库。我XDocument用来解析文件,但有一个问题:
XDocument
XML 示例:
<a b="1">dfas</a> <a b="2">qwsd</a> <a b="3">egfs</a> <a b="4">ghfg</a>
我的代码:
XElement tag; record.A = tag.Element("a").Value;
我需要导入记录。具有属性 b=4 的元素的值。我该怎么做?谢谢!
record.A = tag.Elements("a").First(a => a.Attribute("b").Value == "4").Value;
或者
record.A = (string)tag.Elements("a").FirstOrDefault(a => a.Attribute("b").Value == "4");
record.A = (string)tag.XPathSelectElement("//a[@b='4']");