2012-09-13 48 views
1

我想根據SdkMessageProcessingStepImageId的值選擇SdkMessageProcessingStepId屬性,請參閱下面的xml,但我很掙扎。linq to xml根據子值選擇父屬性

<SdkMessageProcessingStep Name="Plugins.OnPreCreateUpdateCar: Update of new_car" SdkMessageProcessingStepId="{00c19595-76fd-e111-9528-005056af005a}"> 
    <PluginTypeName>Plugins.OnPreCreateUpdateCar, Plugins, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f59c17e3653dba0</PluginTypeName> 
    <PrimaryEntity>new_car</PrimaryEntity> 
    <AsyncAutoDelete>0</AsyncAutoDelete> 
    <InvocationSource>1</InvocationSource> 
    <Mode>0</Mode> 
    <Rank>1</Rank> 
    <SdkMessageId>{20bebb1b-ea3e-db11-86a7-000a3a5473e8}</SdkMessageId> 
    <EventHandlerTypeCode>4602</EventHandlerTypeCode> 
    <Stage>20</Stage> 
    <IsCustomizable>1</IsCustomizable> 
    <IsHidden>0</IsHidden> 
    <SupportedDeployment>0</SupportedDeployment> 
    <SdkMessageProcessingStepImages> 
    <SdkMessageProcessingStepImage Name="PreImageCar"> 
     <SdkMessageProcessingStepImageId>{e3c5bcb1-76fd-e111-9528-005056af005a}</SdkMessageProcessingStepImageId> 
     <EntityAlias>PreImageCar</EntityAlias> 
     <ImageType>0</ImageType> 
     <MessagePropertyName>Target</MessagePropertyName> 
     <IsCustomizable>1</IsCustomizable> 
    </SdkMessageProcessingStepImage> 
    </SdkMessageProcessingStepImages> 
</SdkMessageProcessingStep> 

我嘗試了各種各樣的東西,例如兩個查詢,連接,但似乎沒有任何工作。最後,我選擇了一個完全不同的解決方案,但知道如何在一個查詢中執行此操作會很方便。

TIA

+0

你能給比如你查詢的,它有助於我們理解你的問題更好 – testCoder

回答

1

我不確定你想要達到的目標。但也許這將幫助:

XElement xmlTree = XElement.Load(source); 

string[] result = xmlTree.Descendants("SdkMessageProcessingStepImageId") 
       .Where(element => element.Value == "{e3c5bcb1-76fd-e111-9528-005056af005a}") 
       .Select(element => element.Parent.Parent.Parent) 
       .Attributes() 
       .Where(attribute => attribute.Name == "SdkMessageProcessingStepId") 
       .Select(attribute => attribute.Value) 
       .ToArray(); 

結果:

{00c19595-76fd-e111-9528-005056af005a} 
+0

感謝該做的伎倆。下次必須記得使用Parent。 – ManyRootsofAllEvil