2

在我的 xml 文件中,我如何 通过 linq to xml 选择元素TagId中的属性值ServiceAssignment

注意:字符串属性中的此 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();
4

4 回答 4

1

您在文档中有命名空间的元素,因此您需要将它们包含在查询中。

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");
于 2012-09-13T18:55:44.463 回答
0

您可以尝试使用此代码

var result = from item in XElement.Load("YourFile.xml").Root.Elements("ServiceAssignment")
             where item.Attribute("TagId") == value
             select item ; 
于 2012-09-13T18:51:37.513 回答
0

您的 XML 包含名称空间,因此您不能只比较全名。
如果你不关心命名空间,你可以使用XName.LocalName

var result = from element in doc.Root.Descendants()
             where element.Name.LocalName == "ServiceAssignment"
             select (int)element.Attribute("TagId");
于 2012-09-13T19:01:15.243 回答
0
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);
于 2012-09-13T19:02:57.113 回答