-2

我有以下嵌套循环

foreach (XmlNode nodeT in root.ChildNodes)
{
    foreach (XmlNode nodeServicio in nodeT.ChildNodes)
    {
        nombreMayusculas = nodeServicio.Name.ToUpper();

        if (nombreMayusculas.Contains("SERVICE"))
        {
            int a = 0;
            int.TryParse(nombreMayusculas.Replace("SERVICE", ""), out a);

            if (a.Equals(Type))
            {
                  //some logic here to process only the matching Xmlnode
            }
        }
    }
}

编辑 有没有办法只循环匹配两个条件的匹配 XmlNodes ?

我尝试使用Wherelinq 方法,但不可用,我已经有了using System.Linq

这是XML

<Clients>
    <AgoraCOR1 Type=\"SP\" Default=\"False\">
        <Connectionstring>CONN_STRING</Connectionstring>
        <Service002>SaveOperationNotification</Service002>
        <Service106>SaveOrderNotification</Service106>
    </AgoraCOR1>
    <SerficorpOrdenes1 Type=\"SP\" Default=\"False\">
        <Connectionstring>CONN_STRING</Connectionstring>
        <Service106>SaveOrderNotification</Service106>
        <Service017>SaveComplementationNotification</Service017>
    </SerficorpOrdenes1>
    <CorrevalCORInterno1 Type=\"SP\" Default=\"False\">
        <Connectionstring>CONN_STRING</Connectionstring>
        <Service002>SaveOperationNotification</Service002>
        <Service074>SaveIndicatorNotification</Service074>
        <Service106>SaveOrderNotification</Service106>
        <Service017>SaveComplementationNotification</Service017>
        <Service072>SalvarNotificacionPreciosDeMercado</Service072>
    </CorrevalCORInterno1>
</Clients>
4

3 回答 3

1

It's not completely clear how your xml looks like, but I suppose you have service nodes with names like service1, service2, etc. You can use regular expression to test if node name matches service type (you can change pattern if xml structure differs):

var xdoc = XDocument.Load(path_to_xml);
Regex regex = new Regex(String.Format("^Service{0}$", Type));
var services = from s in xdoc.Root.Elements().Elements()                   
               where regex.IsMatch(s.Name.LocalName)
               select s;

Alternative solution with XPath (you should add System.Xml.XPath) namespace:

var xpath = String.Format("Clients/*/*[name() = 'Service{0}']", Type);
var services = xdoc.XPathSelectElements(xpath);

Same solution with old XmlDocument class:

XmlDocument doc = new XmlDocument();
doc.Load(path_to_xml);

foreach(XmlNode nodeServicio in doc.SelectNodes(xpath)) // xpath same as above
   // use nodeServicio
于 2013-09-09T14:32:25.673 回答
0

您可以使用 Linq - 但它具有与您已经完成的相同的性能配置文件:

var nodesToProcess = root
    .ChildNodes
    .ChildNodes
    .Where(n => n.Name.ToUpper().Contains("SERVICE")));

foreach(var node in nodesToProcess)
{
    ....
}
于 2013-09-09T13:57:38.467 回答
0

第二个循环可以重写如下。

var result = from nodeServicio in nodeT.ChildNodes
             where nodeServicio.Name.ToUpper().Contains("SERVICE")
             select nodeServicio;//or something else

编辑:

根据您发布的代码.. 我猜子节点的值看起来像 SERVICE123、12SERVICE 等

int type = 5; //integer
if (nombreMayusculas.Contains("SERVICE"))
{
    int a = 0;
    int.TryParse(nombreMayusculas.Replace("SERVICE", ""), out a);

    //You cannot use Equals here.. used only for string comparison
    if (a == type))
    {
        //some logic here to process only the matching Xmlnode
    }
}
于 2013-09-09T14:01:49.903 回答