xpath просто выберите соответствующие узлы - PullRequest
0 голосов
/ 24 января 2020

Привет, у меня есть xml файлы, я пытаюсь использовать xpath, получая значение атрибута "mapfile" из xml (показано ниже), а затем получая все значения "ключа" из внешнего mapfile, с которым я могу справиться без проблем.

Затем мне нужно получить все "ключевые" числа из файла xml ниже, которые соответствуют этому файлу карты. Проблема в том, что я могу вытащить их все, но мне нужно вытащить их и проверить по каждому файлу отдельно, а не по целому лоту.

xml разделен на «разделы списка» каждый «раздел списка» имеет файл карты. Я хочу проверить значения «ключа» в «разделе списка» по отношению к значениям «ключа» в файле карты.

так что я попробовал:

//get the list of mapfiles
XmlNodeList mapfiles = xmlDoc.SelectNodes("//a:listsection/a:views/a:explodeddiagram", nsmgr);

//get the mapfiles
foreach (XmlNode mapfile in mapfiles)
{
            string mapfilename = mapfile.Attributes["mapfile"].Value;

        //load the mapfile and read it and get the key values from it.
        XmlDocument xmlDockey = new XmlDocument();
        xmlDochot.Load(parentdir + "\\" + mapfilename);
        XmlNodeList xmlkeyfrommapfile = xmlDockey.SelectNodes("//*[name()='area'][@key]");

        foreach (XmlNode keynode in xmlkeyfrommapfile)
        {
                string keyTextmapfile = keynode.Attributes["key"].Value;
                //So here i've got all the values I need for each "mapfile"
                list1.Add(keyTextmapfile );
        }

}

 //now I get the values of the "key" from the xml file (xml shown below)
 //get the values for the keys from the list file (xml shown below)
 XmlNodeList keysfromlistfile = xmlDoc.SelectNodes("//*[name()='key']");
 foreach (XmlNode keyfromlistfile in keysfromlistfile)
 {
                            string keyTextlistfile = keyfromlistfile.InnerText;
                            list2.Add(keyTextlistfile);
 }

Итак, чтобы повторить: Я получаю всю партию. Я просто хочу проверить каждый файл на наличие «ключевых» значений в одном и том же «разделе списка». Существует несколько «разделов списка», иногда превышающих 20.

<Thislist>
 <listsection>
  <views>
   <explodeddiagram title="Some image title1" graphicfile="SomeFilename1.tif" mapfile="SomeFilename1.xml" />
  </views>
   <rows>
    <row>
     <key>1</key>
     <remark>(Generic Remark 1)</remark>
        <part>
         <number>25376.0000</number>
         <description>Generic Description</description>
       </part>
     <qty>1</qty>
    </row>
    <row>
    <key>2</key>
     <remark>(Generic Remark 4)</remark>
        <part>
        <number>253767890002</number>
        <description>Generic Description</description>
       </part>
     <qty>1</qty>
    </row>
    <row>
     <key>2</key>
     <remark>(Generic Remark 3)</remark>
       <part>
        <number>253764560001</number>
        <description>Generic Description</description>
       </part>
     <qty>1</qty>
    </row>
    <row>
     <key>3</key>
     <remark>(Generic Remark 5)</remark>
       <part>
        <number>209213231004</number>
        <description>Generic Description</description>
       </part>
     <qty>1</qty>
    </row>
   </rows>
 </listsection>
<listsection>
  <views>
    <explodeddiagram title="Some image title2" graphicfile="SomeFilename2.tif" mapfile="SomeFilename2.xml" />
  </views>
   <rows>
    <row>
     <key>1</key>
     <remark>(Generic Remark 6)</remark>
       <part>
        <number>25376656560000</number>
        <description>Cover Assy, Top SST</description>
       </part>
     <qty>1</qty>
    </row>
    <row>
     <key>2</key>
     <remark>(Generic Remark 7)</remark>
       <part>
        <number>2537677902</number>
        <description>Generic Description</description>
       </part>
     <qty>1</qty>
    </row>
    <row>
     <key>2</key>
     <remark>(Generic Remark 8)</remark>
       <part>
        <number>25376457881</number>
        <description>Generic Description</description>
       </part>
     <qty>1</qty>
    </row>
    <row>
     <key>3</key>
     <remark>(Generic Remark 9)</remark>
       <part>
        <number>209288004</number>
        <description>Generic Description</description>
       </part>
     <qty>1</qty>
    </row>
   </rows>
 </listsection>
</Thislist>

Пример файла карты

<?xml version="1.0" encoding="utf-8"?>
<mapfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="hotspot.xsd">
  <map name="SomeMapName">
    <area shape="rect" coords="169,210,199,252" key="3" />
    <area shape="rect" coords="2094,881,2124,924" key="1" />
    <area shape="rect" coords="2094,1031,2124,1074" key="2" />
    <area shape="rect" coords="2094,1145,2124,1187" key="6" />
  </map>
</mapfile>

1 Ответ

0 голосов
/ 28 января 2020

Я понял, мне нужно было

  1. Выбрать каждый узел раздела в этом списке xml файл
  2. Получить раздел foreach со значениями mapfile в файле Thislist xml.
  3. Считать все значения ключей в mapfiles для этого раздела в список
  4. Получить все значения ключей из файла Thislist xml для этого раздела в список
  5. Сравнить два списка и посмотрите, есть ли пропавшие
  6. Если пропущенные есть, отметьте его

                  // namespace
                    var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
                    nsmgr.AddNamespace("a", "http://tempuri.org/XMLSchema.xsd");
                       //get every section in "Thislist" xml file.
                    XmlNodeList sections = xmlDoc.SelectNodes("//a:section", nsmgr);
                    foreach (XmlNode section in sections)
                    {
                //Get all the explodeddiagrams out for the section
                        XmlNodeList Views = section.SelectNodes("a:views/a:explodeddiagram", nsmgr);
    
    
                        foreach (XmlNode ExplodedDiagram in Views)
                        {
                            //get the mapfile attribute which gives us the explodeddiagram filename
                            string mapfile = ExplodedDiagram.Attributes["mapfile"].Value;
                            //load the mapfile as xmlDocmap
                            XmlDocument xmlDocmap = new XmlDocument();
                            xmlDocmap.Load(mapfile);
    
                            //go through everymap file in the section and get the key values.
                            XmlNodeList xmlmapkeylist = xmlDocmap.SelectNodes("//*[name()='area'][@key]");
                            foreach (XmlNode areakey in xmlmapkeylist)
                            {
                                string keyText = areakey.Attributes["key"].Value;
                                //add the key values from the mapfile into a list
                                listmapkeys.Add(keyText);
                            }
                        }
    
                      //get all the key values from the "Thislist" xml file.
                        XmlNodeList keys = section.SelectNodes("a:partslistsectionrows/a:partslistsectionrow/a:key", nsmgr);
                        foreach (XmlNode ListKey in keys)
                        {
    
                            //add the key values from the each section in "Thislist"xml into a list
                            listsectionkeys.Add(ListKey.InnerText);
                        }
                    }
    
                    foreach (string mapkey in listmapkeys)
                    {
                    //if the section doesn't contain a key value present in the mapfile. 
                        if(!listsectionkeys.Contains(mapkey))
                        {//output the differences or discrepencies this is going to listBox1 in this case
                            listBox1.Items.Add(hotspot + " not in partslist");
                        }
    
                    }
    
                }
    
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...