简体   繁体   English

C#XML如何通过属性检索字段的innerText?

[英]C# XML How to retrive innerText of field by attribute?

Here is the XML sample: 这是XML示例:

  <?xml version="1.0" ?> 
  <XMLScreen xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <CX>80</CX> 
  <CY>24</CY> 
  <Formatted>true</Formatted> 
  <Field>
  <Location position="1" left="1" top="0" length="69" /> 
  <Attributes Base="226" Protected="false" FieldType="High" /> 
  *SDC SCHEDULING CATEGORY UPDATE 
  </Field>
  </XMLScreen>

I want to retrive the Inner text of each field based on its Location position . 我想根据其Location position检索每个字段的内部文本。

What I have so far is: 到目前为止,我有:

  XmlDocument xmlDoc = new XmlDocument();
  xmlDoc.LoadXml(myEm.CurrentScreenXML.GetXMLText());
  XmlNodeList fields = xmlDoc.GetElementsByTagName("Field");

  MessageBox.Show("Field spot: " + i + " Contains: " + fields[i].InnerText);

And I want to be able to just extract the inner text of the field by passing in a number of the location position. 而且我希望能够通过传递多个位置位置来提取字段的内部文本。 So if I say foo[i] I want to be able to get the innertext 因此,如果我说foo[i]我希望能够获得内文

*SDC SCHEDULING CATEGORY UPDATE * SDC计划分类更新

Something like that, with XDocument instead of XmlDocument (well, if you're not in .net 3.5 or higher, we'll have a problem). 像这样,用XDocument代替XmlDocument(好吧,如果您不在.net 3.5或更高版本中,我们会遇到问题)。

private string GetTextByLocationId(XDocument document, int id)
{
     var field = document.Descendants("Field").FirstOrDefault(m => m.Element("Location").Attribute("position").Value == id.ToString());
     if (field == null) return null;
     return field.Value;
}

and usage 和用法

var xDocument = XDocument.Load(<pathToXmlFile or XmlReader or string or ...>);
var result = GetTextByLocationId(xDocument, 1);

EDIT 编辑

or if you want a dictionary with :key = position / value = text 或者如果您想使用:key = position / value = text的字典

private static Dictionary<int, string> ParseLocationAndText(XDocument document)
        {
            var fields = document.Descendants("Field");
            return fields.ToDictionary(
                 f => Convert.ToInt32(f.Element("Location").Attribute("position").Value),
                 f => f.Value);
        }

You should use a xpath search query : 您应该使用xpath搜索查询:

  XmlDocument xmlDoc = new XmlDocument();
  xmlDoc.LoadXml(xml);
  int nodeId = 4;
  XmlNode node = xmlDoc.SelectSingleNode(String.Format(@"//Location[@position='{0}']", nodeId));
  if (node != null)
  {
      String field = node.ParentNode.InnerText;
  }

Try, 尝试,

XElement root = XElement.Parse(myEm.CurrentScreenXML.GetXMLText());
XElement field = root.XPathSelectElement(
                    string.Format("Field[Location/@position='{0}']", 1));
string text = field.Value;

You will need to use the following using to use XPath with XElements. 您需要使用以下用法将XPath与XElements结合使用。

using System.Xml.XPath;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM