简体   繁体   English

如何以编程方式找到特定的 XML 元素?

[英]How can I find a specific XML element programmatically?

I have this chunk of XML我有这块 XML

<EnvelopeStatus>

  <CustomFields>
     <CustomField>
        <Name>Matter ID</Name>
        <Show>True</Show>
        <Required>True</Required>
        <Value>3</Value>
     </CustomField>
     <CustomField>
        <Name>AccountId</Name>
        <Show>false</Show>
        <Required>false</Required>
        <Value>10804813</Value>
        <CustomFieldType>Text</CustomFieldType>
     </CustomField>

I have this code below:我在下面有这段代码:

// TODO find these programmatically rather than a strict path.
var accountId = envelopeStatus.SelectSingleNode("./a:CustomFields", mgr).ChildNodes[1].ChildNodes[3].InnerText;
var matterId = envelopeStatus.SelectSingleNode("./a:CustomFields", mgr).ChildNodes[0].ChildNodes[3].InnerText;

The problem is, sometimes the CustomField with 'Matter ID' might not be there.问题是,有时带有“Matter ID”的 CustomField 可能不存在。 So I need a way to find the element based on what 'Name is', ie a programmatic way of finding it.所以我需要一种方法来根据“名称是什么”来查找元素,即以编程方式查找它。 I can't rely on indexes being accurate.我不能依赖索引是准确的。

You can use this code to read innertext from a specific element:您可以使用此代码从特定元素读取内部文本:

XmlDocument doc = new XmlDocument();
doc.Load("your.xml");

XmlNodeList Nodes= doc.SelectNodes("/EnvelopeStatus/CustomField");
if (((Nodes!= null) && (Nodes.Count > 0)))
                {
                    foreach (XmlNode Level1 in Nodes)
                    {
                          if (Level1.ChildNodes[1].Name == "name")
                            {
                             string text = Convert.ToInt32(Level1.ChildNodes[1].InnerText.ToString());
                           }
                        
                        

                    }
                }

You can often find anything in a XML document by utilizing the XPath capabilities that is available directly in the .NET Framework versions.通过利用 .NET 框架版本中直接提供的 XPath 功能,您通常可以在 XML 文档中找到任何内容。

Maybe create a small XPath parser helper class也许创建一个小的 XPath 解析器助手 class

public class EnvelopeStatusParser
{
    public XmlNodeList GetNodesWithName(XmlDocument doc, string name)
    {
        return doc.SelectNodes($"//CustomField[Name[text()='{name}']]");
    }
}

and then call it like below to get all CustomFields which have a Name that equals what you need to search for然后像下面这样调用它以获取所有名称等于您需要搜索的名称的自定义字段

// Creating the XML Document in some form - here reading from file
XmlDocument doc = new XmlDocument();
doc.Load(@"envelopestatus.xml");

var parser = new EnvelopeStatusParser();

var matchingNodes = parser.GetNodesWithName(doc, "Matter ID");
Console.WriteLine(matchingNodes);

matchingNodes = parser.GetNodesWithName(doc, "NotHere");
Console.WriteLine(matchingNodes);

There exist numerous XPath cheat sheets around - like this one from LaCoupa - xpath-cheatsheet which can be quiet helpful to fully utilize XPath on XML structures.周围有许多 XPath 备忘单 - 就像 LaCoupa 的这个 - xpath-cheatsheet可以安静地帮助在 XML 结构上充分利用 XPath。

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

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