簡體   English   中英

通過linq to xml選擇屬性的值

[英]select value of attribute by linq to xml

在我的xml文件中,如何通過linq to xml選擇ServiceAssignment元素中的屬性TagId的值

注意:此xml位於字符串屬性中,不在xml文件中

<AnchoredXml xmlns="urn:schema:Microsoft.Rtc.Management.ScopeFramework.2008" SchemaWriteVersion="1">
  <Key ScopeClass="Global">
    <SchemaId Namespace="urn:schema:Microsoft.Rtc.Management.Settings.ServiceAssignment.2008" ElementName="ServiceAssignments" />
    <AuthorityId Class="Host" InstanceId="00000000-0000-0000-0000-000000000000" />
  </Key>
  <Dictionary Count="1">
    <Item>
      <Key />
      <Value Signature="2ffb6b0d-0239-4016-b08b-40520d1687ff">
        <ServiceAssignments xmlns="urn:schema:Microsoft.Rtc.Management.Settings.ServiceAssignment.2008">
          <ServiceAssignment TagId="659550892">
            <Component Name="Registrar">
              <ServiceId xmlns="urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008" SiteId="1" RoleName="Registrar" Instance="1" />
            </Component>
            <Component Name="PresenceFocus">
              <ServiceId xmlns="urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008" SiteId="1" RoleName="UserServices" Instance="1" />
            </Component>
          </ServiceAssignment>
          <ServiceAssignment TagId="911048693">
            <Component Name="Registrar">
              <ServiceId xmlns="urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008" SiteId="1" RoleName="Registrar" Instance="2" />
            </Component>
            <Component Name="PresenceFocus">
              <ServiceId xmlns="urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008" SiteId="1" RoleName="UserServices" Instance="2" />
            </Component>
          </ServiceAssignment>
        </ServiceAssignments>
      </Value>
    </Item>
  </Dictionary>
</AnchoredXml>

我嘗試此代碼,但給我一個空異常

var MyList = doc.Root.Elements("ServiceAssignment").Select(c=>c.Attribute(("TagId")).Value).ToList();

您在文檔中有命名空間元素,因此您需要在查詢中包括它們。

XNamespace itemNs = "urn:schema:Microsoft.Rtc.Management.ScopeFramework.2008";
XNamespace assignmentNs = "urn:schema:Microsoft.Rtc.Management.Settings.ServiceAssignment.2008";
var query =
    from item in doc.Descendants(itemNs + "Item")
    from assignment in item.Descendants(assignmentNs + "ServiceAssignment")
    select (long)assignment.Attribute("TagId");

您可以嘗試使用此代碼

var result = from item in XElement.Load("YourFile.xml").Root.Elements("ServiceAssignment")
             where item.Attribute("TagId") == value
             select item ; 

您的XML包含名稱空間,因此您不能只比較全名。
如果您不關心名稱空間,則可以使用XName.LocalName

var result = from element in doc.Root.Descendants()
             where element.Name.LocalName == "ServiceAssignment"
             select (int)element.Attribute("TagId");
string xmlString =
                @"<AnchoredXml xmlns='urn:schema:Microsoft.Rtc.Management.ScopeFramework.2008' SchemaWriteVersion='1'>
  <Key ScopeClass='Global'>
    <SchemaId Namespace='urn:schema:Microsoft.Rtc.Management.Settings.ServiceAssignment.2008' ElementName='ServiceAssignments' />
    <AuthorityId Class='Host' InstanceId='00000000-0000-0000-0000-000000000000' />
  </Key>
  <Dictionary Count='1'>
    <Item>
      <Key />
      <Value Signature='2ffb6b0d-0239-4016-b08b-40520d1687ff'>
        <ServiceAssignments xmlns='urn:schema:Microsoft.Rtc.Management.Settings.ServiceAssignment.2008'>
          <ServiceAssignment TagId='659550892'>
            <Component Name='Registrar'>
              <ServiceId xmlns='urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008' SiteId='1' RoleName='Registrar' Instance='1' />
            </Component>
            <Component Name='PresenceFocus'>
              <ServiceId xmlns='urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008' SiteId='1' RoleName='UserServices' Instance='1' />
            </Component>
          </ServiceAssignment>
          <ServiceAssignment TagId='911048693'>
            <Component Name='Registrar'>
              <ServiceId xmlns='urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008' SiteId='1' RoleName='Registrar' Instance='2' />
            </Component>
            <Component Name='PresenceFocus'>
              <ServiceId xmlns='urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008' SiteId='1' RoleName='UserServices' Instance='2' />
            </Component>
          </ServiceAssignment>
        </ServiceAssignments>
      </Value>
    </Item>
  </Dictionary>
</AnchoredXml>";

var doc = XDocument.Parse(xmlString);       
var TagIds = doc.Descendants()
                .Elements()
                .Where(e => 
                            e.HasAttributes && 
                            e.Name.LocalName.Equals("ServiceAssignment") && 
                            e.Attribute("TagId") != null)
                .Select(e => e.Attribute("TagId").Value);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM