[英]xpath just select the relevant nodes
嗨,我有一些 xml 文件,我正在尝试通过从 xml 中获取“mapfile”属性值(如下所示)来使用 xpath,然后从外部映射文件中获取所有“键”值,我不能这样做问题。
然后我需要从下面的 xml 文件中获取与此映射文件相对应的所有“关键”数字。 问题是我可以将它们全部取出,但我需要将它们取出并单独检查每个文件,而不是全部。
xml 被分成“listsections”,每个“listsection”都有一个映射文件。 我想根据映射文件中的“键”值检查“列表部分”中的“键”值。
所以我试过的是
//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);
}
所以重申:我得到了全部。 我只想在同一个“listsection”中检查每个文件的“key”值,有时有多个“listsections”超过 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>
我想通了,我需要
如果有一些丢失的标记出来
// 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"); } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.