基于另一个属性值的XMLNodeList中的XMLNode中的属性值

本文关键字:属性 XMLNode 中的 XMLNodeList 另一个 | 更新日期: 2023-09-27 18:08:16

我有一个XmlNodeList与3 XmlElement。

我正试图根据不同的属性值从节点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>

因此,在上面的节点中,我希望能够测试值"rId2"是否在innerXml中,如果它返回值https://myserver/image1.jpg

请问怎么做?

基于另一个属性值的XMLNodeList中的XMLNode中的属性值

不要使用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);

终于破解了…

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