XML子元素解析

时间:2014-06-23 10:41:42

标签: c# .net xml

我正在尝试从命名空间中读取值,但它无法正常工作。我使用以下代码将后代放入列表中。

       XDocument doc = XDocument.Parse(QueryParmeterString);
       XNamespace ns = XNamespace.Get("http://Microsoft.EnterpriseManagement.Core.Criteria/");
       var expressions = (from ds in doc.Descendants(ns + "Expression")
                       select ds).ToList();


           foreach (var foo in expressions)
                   {
                       string baa = foo.Attributes["ValueExpressionLeft"].Value;
                   }


    }

此时我无法访问每个对象。我需要访问每个属性的左右值。我有一个强类型对象,可以存储值。

        <Expression xmlns="http://Microsoft.EnterpriseManagement.Core.Criteria/">
          <And>
            <Expression>
              <SimpleExpression>
                <ValueExpressionLeft>
                  <Property>$Context/Property[Type='CoreIncident!System.WorkItem.Incident']/TierQueue$</Property>
                </ValueExpressionLeft>
                <Operator>Equal</Operator>
                <ValueExpressionRight>
                  <Value>$MPElement[Name="IncidentTierQueuesEnum.Tier2"]$</Value>
                </ValueExpressionRight>
              </SimpleExpression>
            </Expression>
            <Expression>
              <SimpleExpression>
                <ValueExpressionLeft>
                  <Property>$Context/Property[Type='CoreIncident!System.WorkItem.Incident']/Status$</Property>
                </ValueExpressionLeft>
                <Operator>NotEqual</Operator>
                <ValueExpressionRight>
                  <Value>$MPElement[Name="CoreIncident!IncidentStatusEnum.Resolved"]$</Value>
                </ValueExpressionRight>
              </SimpleExpression>
            </Expression>
            <Expression>
              <SimpleExpression>
                <ValueExpressionLeft>
                  <Property>$Context/Property[Type='CoreIncident!System.WorkItem.Incident']/Status$</Property>
                </ValueExpressionLeft>
                <Operator>NotEqual</Operator>
                <ValueExpressionRight>
                  <Value>$MPElement[Name="CoreIncident!IncidentStatusEnum.Closed"]$</Value>
                </ValueExpressionRight>
              </SimpleExpression>
            </Expression>
            <Expression>
              <UnaryExpression>
                <ValueExpression>
                  <GenericProperty Path="$Context/Path[Relationship='WorkItem!System.WorkItemAssignedToUser' SeedRole='Source']$">Id</GenericProperty>
                </ValueExpression>
                <Operator>IsNull</Operator>
              </UnaryExpression>
            </Expression>
          </And>
        </Expression>

1 个答案:

答案 0 :(得分:2)

试试这个可能会对你有所帮助:

XDocument doc = XDocument.Parse(QueryParmeterString);
XNamespace ns = XNamespace.Get("http://Microsoft.EnterpriseManagement.Core.Criteria/");
var expressions = (from ds in doc.Root.Descendants(ns + "Expression")
                           select ds).ToList();

        foreach (var foo in expressions)
        {
            XElement expressionTypeElement = foo.FirstNode  as XElement;
            if (expressionTypeElement != null)
            {
                if (expressionTypeElement.Name.LocalName == "SimpleExpression")
                {
                    XElement valueExpressionLeft = expressionTypeElement.Element(ns +"ValueExpressionLeft");
                    XElement valueExpressionRight = expressionTypeElement.Element(ns +"ValueExpressionRight");
                    string operator = expressionTypeElement.Element(ns + "Operator").Value;
                    // do your job here 
                    //....
                }
                else if (expressionTypeElement.Name.LocalName == "UnaryExpression")
                {
                    XElement valueExpression = expressionTypeElement.Element(ns +"ValueExpression");
                    // do your job here
                    //....
                }
            }
        }