XPath. NET CORE - PullRequest
       167

XPath. NET CORE

0 голосов
/ 07 августа 2020

У меня есть этот кусок 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>

У меня есть этот код ниже:

            
            // 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;

Проблема в том, что иногда CustomField с 'Matter ID' может не быть там. Поэтому мне нужен способ найти элемент, основанный на том, что такое «Имя», т.е. программный способ его поиска. Я не могу полагаться на точность индексов.

Всем спасибо!

1 Ответ

0 голосов
/ 07 августа 2020

Вы можете использовать этот код для чтения внутреннего текста из указанного c элемента:

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());
                           }
                        
                        

                    }
                }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...