简体   繁体   English

无法提取元素(通过元素名称AND属性)

[英]Can't extract Elements (by Element name AND attribute)

I have the following XML sample which I need to parse in order to extract parameters: 我有以下XML示例,我需要对其进行分析以提取参数:

    <?xml version="1.0" encoding="UTF-8"?>
<bulkCmConfigDataFile xmlns:un="utranNrm.xsd"  xmlns:xn="genericNrm.xsd" xmlns:gn="geranNrm.xsd"  xmlns="configData.xsd" 
 xmlns:es="Vendor1SpecificAttributes.1.0.xsd">
    <fileHeader fileFormatVersion="1.0" vendorName="Vendor1"/>
    <configData dnPrefix="Undefined">
        <xn:SubNetwork id="ONRM_ROOT_MO_R">
            <xn:SubNetwork id="RNC0001">
                <xn:MeContext id="BLABLA">
                </xn:MeContext>
                <xn:MeContext id="MACHIN">
                </xn:MeContext>
                <xn:MeContext id="RNC0001">
                    <xn:VsDataContainer id="RNC0001">
                    </xn:VsDataContainer>
                    <xn:ManagedElement id="1">
                        <un:RncFunction id="1">
                            <un:UtranCell id="111111A">
                                <un:attributes>
                                    <un:uarfcnUl>9800</un:uarfcnUl>
                                    <un:uarfcnDl>10700</un:uarfcnDl>
                                </un:attributes>
                                <xn:VsDataContainer id="111111A">
                                    <es:Position>
                                    <es:latitudeSign>1</es:latitudeSign>
                                    <es:latitude>3070727</es:latitude>
                                    <es:longitude>8786820</es:longitude>
                                    </es:Position>
                                </xn:VsDataContainer>
                                <xn:VsDataContainer id="1">
                                </xn:VsDataContainer>
                            </un:UtranCell>
                            <un:UtranCell id="111111B">
                                 <un:attributes>
                                    <un:uarfcnUl>9800</un:uarfcnUl>
                                    <un:uarfcnDl>10700</un:uarfcnDl>
                                </un:attributes>
                                <xn:VsDataContainer id="111111B">
                                    <es:Position>
                                    <es:latitudeSign>1</es:latitudeSign>
                                    <es:latitude>3070555</es:latitude>
                                    <es:longitude>8786666</es:longitude>
                                    </es:Position>
                                </xn:VsDataContainer>
                                <xn:VsDataContainer id="1">
                                </xn:VsDataContainer>
                            </un:UtranCell>
                        </un:RncFunction>
                    </xn:ManagedElement>
                </xn:MeContext>
            </xn:SubNetwork>
        </xn:SubNetwork>
    </configData>
    <fileFooter dateTime="2011-11-28T08:38:45Z"/>
</bulkCmConfigDataFile>             

So far I've only been able to retrieve the first element by name for a given namespace: 到目前为止,我只能通过名称获取给定名称空间的第一个元素:

XNamespace xn = "genericNrm.xsd";
XNamespace un = "utranNrm.xsd";
var test1 = xmldoc.Descendants(xn + "MeContext").FirstOrDefault();

which will only give me the first Element "MeContext" (in this example this will return the MeContext with id=BLABLA). 这只会给我第一个元素“ MeContext”(在本示例中,它将返回id = BLABLA的MeContext)。

If I try the following instead 如果我尝试以下方法

XNamespace xn = "genericNrm.xsd";
XNamespace un = "utranNrm.xsd";
var test1 = xmldoc.Descendants(xn + "MeContext");

test1 will be null ... test1将为null ...

1 - My first question is how do I retrieve a specific element using its "id" attribute (in this specific example I'm looking for the id="RNC0001"). 1-我的第一个问题是如何使用其“ id”属性检索特定元素(在此特定示例中,我正在寻找id =“ RNC0001”)。 I tried many things coming from stackoverflow including the following: 我尝试了许多来自stackoverflow的东西,包括:

XNamespace xn = "genericNrm.xsd";
IEnumerable<XElement> utrancells =
                            xmldoc.Root
                                    .Elements(xn + "MeContext")
                                    .Where(el => (string)el.Attribute("id") == "RNC0001");

which only returns null values. 仅返回空值。

2 - The second problem is how to retrieve a collection of elements from the <xn:MeContext id="RNC0001"></xn:MeContext> ? 2-第二个问题是如何从<xn:MeContext id="RNC0001"></xn:MeContext>检索元素的集合? I would like to get all the <un:UtranCell id="XXXXX"></un:UtranCell> (and content) in a collection so I can extract data from each of them (each represent a different entity). 我想获取<un:UtranCell id="XXXXX"></un:UtranCell>所有<un:UtranCell id="XXXXX"></un:UtranCell> (和内容),以便我可以从每个集合中提取数据(每个代表不同的实体)。 For instance I need to retrieve 例如我需要检索

<es:latitude>3070555</es:latitude>
<es:longitude>8786666</es:longitude>

from each <un:UtranCell id="XXXXX"></un:UtranCell> 来自每个<un:UtranCell id="XXXXX"></un:UtranCell>

For this I tried (as a test): 为此,我尝试(作为测试):

XNamespace un = "utranNrm.xsd";
var test3 = xmldoc.Elements(un + "UtranCell");
var test4 = test1.Elements(un + "UtranCell");

and again it returns only null values ... 再一次它只返回空值...

Edit for Daniel: The real code is: 编辑Daniel:真正的代码是:

public void parseFile()
{
    XNamespace xn = "genericNrm.xsd";
    XNamespace un = "utranNrm.xsd";
    XNamespace cd = "configData.xsd";

    var test1 = xmldoc.Descendants(xn + "MeContext").FirstOrDefault();
    var test1bis = xmldoc.Descendants(xn + "MeContext");
}

This gives me: test1 = <xn:MeContext id="BLABLA" xmlns:xn="genericNrm.xsd"></xn:MeContext> 这给了我:test1 = <xn:MeContext id="BLABLA" xmlns:xn="genericNrm.xsd"></xn:MeContext>

test1bis = {System.Xml.Linq.XContainer.GetDescendants} | test1bis = {System.Xml.Linq.XContainer.GetDescendants} | name = null 名称=空

The problem in both cases is that you are using Elements instead of Descendants . 两种情况下的问题都是您使用的是Elements而不是Descendants The difference is that Elements only returns those elements that are immediate children - ie one level deep, whereas Descendants searches the entire subtree. 不同之处在于Elements仅返回那些直接子元素(即一层深),而Descendants搜索整个子树。 Try this instead: 尝试以下方法:

IEnumerable<XElement> utrancells =
    xmldoc.Root
          .Descendants(xn + "MeContext")
          .Where(el => (string)el.Attribute("id") == "RNC0001");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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