简体   繁体   中英

xpath just select the relevant nodes

Hi i've got some xml files, I'm trying to use xpath by getting the "mapfile" attribute value from the xml (shown below), then getting all the "key" values from the external mapfile, which I can do no problem.

Then I need to get all the "key" numbers from the xml file below that correspond to this mapfile. The problem is I can get them all out, but I need to get them out and check against each file individually, not the whole lot.

the xml is split up into "listsections" each "listsection" has a map file. I want to check the "key" values in the "listsection" against the "key" values in the map file.

so what i've tried is

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

So to reitterate: I get the whole lot. I just want to check each file for the "key" values in the same "listsection" There's multiple "listsections" sometimes over 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>

Example of mapfile

<?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>

I figured it out, I needed to

  1. Select each section node in Thislist xml file
  2. Get the mapfile values foreach section in the Thislist xml file.
  3. Read all the key values in the mapfiles for that section into a list
  4. Get all the key values from Thislist xml file for that section into a list
  5. Compare the two lists and see if there are any that are missing
  6. If there are some that are missing flag it up

     // 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"); } } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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