猿问

如何在 C# 中从 XML 获取属性

我得到输出 xml 格式,可以访问约会-nbr,但我不能 eqid。我怎样才能获得 slot-start、slot-end、eqid。


 > <appointment-nbr>494</appointment-nbr> <slot

 > slot-start="2018-07-16T12:31:00" slot-end="2018-07-16T13:00:00" />

 > <appointment requires-xray="false" /> <container eqid="ASWU2705080" />

这是我的代码:


foreach (XmlNode node in appointmentsresponce){

XmlElement flightEle = (XmlElement)node;

XmlNodeList appointmentnbr = flightEle.GetElementsByTagName("appointment-nbr");

XmlNodeList containerNodeList = flightEle.GetElementsByTagName("container");

}


慕仙森
浏览 283回答 2
2回答

慕村9548890

尝试(因为您没有发布完整数据,所以我有点猜测):foreach (XElement level1Element in XElement.Load(@"your_file.xml").Elements("appointment-nbr")){&nbsp; &nbsp; foreach (XElement level2Element in level1Element.Elements("slot"))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(level1Element.Attribute("slot-start").Value);&nbsp; &nbsp; }}

饮歌长啸

简单调用GetAttribute("AttributeName")您的XmlElement所以:var slotXml = appointmentsresponce.SelectSingleNode("//slot")var startAttr = slotXml.GetAttribute("slot-start")var endAttr = slotXml.GetAttribute("slot-end")var containerXml = appointmentsresponce.SelectSingleNode("//container ")var eqidAttr = containerXml .GetAttribute("eqid")
随时随地看视频慕课网APP
我要回答