2015-08-15 87 views
0

我有一个带有3个XmlElement的XmlNodeList。基于另一个属性值的来自XMLNodeList的XMLNode中的属性值

我试图拉从基于不同的属性值的节点InnerXML属性值...

中的XmlElement InnerXML之一的一个例子是这样的:

<p:nvPicPr xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"><p:cNvPr id="5" name="Content Placeholder 4" title="https://myserver/image1.jpg" /><p:cNvPicPr><a:picLocks noGrp="1" noChangeAspect="1" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" /></p:cNvPicPr><p:nvPr><p:ph idx="1" /></p:nvPr></p:nvPicPr><p:blipFill xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"><a:blip r:embed="rId2" cstate="print" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><a:extLst><a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}"><a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0" /></a:ext></a:extLst></a:blip><a:stretch xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><a:fillRect /></a:stretch></p:blipFill><p:spPr xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"><a:xfrm xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><a:off x="7972166" y="1690688" /><a:ext cx="3830596" cy="2154710" /></a:xfrm></p:spPr> 

所以, abve节点我希望能够测试值“rId2”是否在innerXml中,并且如果它返回值https://myserver/image1.jpg

请执行此操作的方法是什么?

回答

0

不要使用InnerXml - 这是一个简单的字符串。使用节点列表和属性集合。

XmlNodeList list = ... // your list with 3 nodes 

XmlNodeList descendants = list[0].ParentNode.SelectNodes("//*"); 

bool imageFound = false; 
bool idFound = false; 

foreach (XmlNode node in descendants) 
{ 
    foreach (XmlAttribute attr in node.Attributes) 
    { 
     if (attr.Value == "https://myserver/image1.jpg") 
      imageFound = true; 
     if (attr.Value == "rId2") 
      idFound = true; 
    } 
} 

Console.WriteLine(imageFound); 
Console.WriteLine(idFound); 
+0

非常感谢帮助 - 但是这并不完全做我需要,我可以不知道如何使它做... 。在3个节点中的1个节点将有一个值= rId2 - 从那个节点开始,然后我尝试检索“title”的值......你能够进一步帮助吗? –

+0

@TrevorDaniel值为'rId2'的属性位于第二个节点。 'title'属性位于第一个节点。这是正确的?对不起,我的英语不好。 –

+0

有3个节点。每个都有一个名为“r:embed”和标题的属性。我想知道如何搜索3个节点,并在知道“r:embed”值时返回“标题”。 –

0

终于解决了......

string oldRelID = part.GetIdOfPart(imagePart); 

        foreach (XmlElement list in pictureNodeList) 
        { 
         XmlNodeList elementList = list.GetElementsByTagName("a:blip"); 

         foreach (XmlNode node in elementList) 
         { 
          foreach (XmlAttribute att in node.Attributes) 
          { 
           if (att.Value == oldRelID) 
           { 
            XmlNodeList trevList = list.GetElementsByTagName("p:cNvPr"); 

            foreach (XmlNode trevnode in trevList) 
            { 
             foreach (XmlAttribute trevatt in trevnode.Attributes) 
             { 
              if (trevatt.Name == "descr") 
              { 
               mapReference = trevatt.Value; 
              } 
             } 
            } 
           } 
          } 
         } 
        } 
相关问题