本文介绍了当命名空间限定符不是子标记的一部分时,按名称空间过滤子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到问题;这是一个示例文件:

Stuck on a problem; here is a sample file:

<?xml version =" 1.0"编码= QUOT; UTF-8英寸standalone =" yes"?>
$
< AcRecBat>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AcRecBat>

 < TrnAck:Acknowledgement xmlns:TrnAck =" www.someack.com ">

       < TransmissionData>

         < Person>&
            <名称>

               < FirstName> Simon< / FirstName>

               < MiddleName> Ann< / MiddleName>

               < LastName> Garfunkel< / LastName>

            < / Name>

            <名称>

               < FirstName> Zee< / FirstName>

               < MiddleName> Zee< / MiddleName>

               < LastName> Top< / LastName>

            < / Name>

         < / Person>

       < / TransmissionData>

 <TrnAck:Acknowledgment xmlns:TrnAck="www.someack.com">
       <TransmissionData>
         <Person>
            <Name>
               <FirstName>Simon</FirstName>
               <MiddleName>Ann</MiddleName>
               <LastName>Garfunkel</LastName>
            </Name>
            <Name>
               <FirstName>Zee</FirstName>
               <MiddleName>Zee</MiddleName>
               <LastName>Top</LastName>
            </Name>
         </Person>
       </TransmissionData>



 < ColTrn:SomeTranscript xmlns:ColTrn =" www.someTranscript.com ">

       < TransmissionData>

         < Person>&
            <名称>

               < FirstName> Chad< / FirstName>

               < MiddleName> Ocho< / MiddleName>

               < LastName> Cinco< / LastName>

            < / Name>

            <名称>

               < FirstName> Brett< / FirstName>

               < MiddleName> Jet< / MiddleName>

               < LastName> Favre< / LastName>

            < / Name>

         < / Person>


 <ColTrn:SomeTranscript xmlns:ColTrn="www.someTranscript.com">
       <TransmissionData>
         <Person>
            <Name>
               <FirstName>Chad</FirstName>
               <MiddleName>Ocho</MiddleName>
               <LastName>Cinco</LastName>
            </Name>
            <Name>
               <FirstName>Brett</FirstName>
               <MiddleName>Jet</MiddleName>
               <LastName>Favre</LastName>
            </Name>
         </Person>

       < / TransmissionData>

       </TransmissionData>

   < / ColTrn:SomeTranscript>

   </ColTrn:SomeTranscript >

< / AcRecBat>

</AcRecBat>

--------------- -------------------------------------------------- -------------------------------------------------- ------------------

-------------------------------------------------------------------------------------------------------------------------------------

如果我只想返回"www.someTranscript.com "命名空间?我尝试了一些事情。要么我什么都不返回,要么我回来,例如所有< person>,包括来自
名称空间的名称空间。

What if I wanted to return only the data contained within the "www.someTranscript.com" namespace? I've tried a few things. Either I return nothing, or I return e.g. all <person>'s, including those from thewww.someack.com namespace too.

我正在使用LinqtoXML和C#

I'm using LinqtoXML with C#

 

 

  ;

 

推荐答案

XNamespace colTrn =" www.someTranscript.com";

XNamespace colTrn = "www.someTranscript.com";

IEnumerable< XElement> dataElements = doc.Root.Elements(colTrn +" SomeTranscript")。元素(" TransmissionData");

IEnumerable<XElement> dataElements = doc.Root.Elements(colTrn + "SomeTranscript").Elements("TransmissionData");

应该这样做,假设"返回......数据"。表示您想要返回根元素的任何ColTrn:SomeTranscript子元素中包含的传输数据元素。如果您只想处理第一个这样的元素,请使用例如

should do, assuming "returning ... the data" means you want to return the Transmission data element(s) contained in any ColTrn:SomeTranscript child elements of the root element. In case you only want to process the first such elements use e.g.

XElement dataElement = doc.Root.Element(colTrn +" SomeTranscript")。Element(" TransmissionData");

XElement dataElement = doc.Root.Element(colTrn + "SomeTranscript").Element("TransmissionData");

如果这不能解决问题,那么请更详细地解释什么是"数据"。 (你正在寻找哪些节点)。

If that does not solve the problem then please explain in more detail what "data" (i.e. which nodes exactly) you are looking for.


这篇关于当命名空间限定符不是子标记的一部分时,按名称空间过滤子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 13:46