Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Linq更新为XML元素值串联问题_C#_Regex_Linq To Xml - Fatal编程技术网

C# 将Linq更新为XML元素值串联问题

C# 将Linq更新为XML元素值串联问题,c#,regex,linq-to-xml,C#,Regex,Linq To Xml,我正在尝试编写一个app.config/web.config纠错应用程序,它将审计我们的开发人员应用程序的错误环境设置。我正在使用LINQtoXML来实现这一点,我遇到了一个障碍 var query = from el in doc.Descendants().Element("SMTPHost") select el; foreach (XElement host in query) { if (Regex.IsMatch(el.Value, "mail.mycomp

我正在尝试编写一个app.config/web.config纠错应用程序,它将审计我们的开发人员应用程序的错误环境设置。我正在使用LINQtoXML来实现这一点,我遇到了一个障碍

var query =
    from el in doc.Descendants().Element("SMTPHost")
    select el;

foreach (XElement host in query)
{
    if (Regex.IsMatch(el.Value, "mail.mycompany.com")
    {
        el.Value = Regex.Replace(el.Value, 
            "mail.mycompany.com", "devmail.mycompany.com");

    }
}
当我运行它时,它会连接正确元素的一个祖先节点的所有子文本值,并删除所有子元素

有没有更好的方法来做这类事情


谢谢

首先,我认为这里没有必要使用正则表达式。您是否知道,由于点的匹配方式,将匹配“mailxmycompanyxcom”?使用String.Contains和String.Replace。当然,如果真正的代码使用真正的模式,那就不同了

不管怎么说,我来谈谈这个问题。在我看来,您一次只对处理一个子节点感兴趣,对吗?如果是,请使用:

var query =  doc.Descendants()
                .Element("SMTPHost")
                .DescendantNodes()
                .OfType<XText>();

foreach (XText textNode in query)
{
    textNode.Value = textNode.Value.Replace("mail.mycompany.com", 
                                            "devmail.mycompany.com");
}
var query=doc.subjects()
.Element(“SMTPHost”)
.DegeneratNodes()的
.of type();
foreach(查询中的XText textNode)
{
textNode.Value=textNode.Value.Replace(“mail.mycompany.com”,
“devmail.mycompany.com”);
}

首先,我认为这里不需要使用正则表达式。您是否知道,由于点的匹配方式,将匹配“mailxmycompanyxcom”?使用String.Contains和String.Replace。当然,如果真正的代码使用真正的模式,那就不同了

不管怎么说,我来谈谈这个问题。在我看来,您一次只对处理一个子节点感兴趣,对吗?如果是,请使用:

var query =  doc.Descendants()
                .Element("SMTPHost")
                .DescendantNodes()
                .OfType<XText>();

foreach (XText textNode in query)
{
    textNode.Value = textNode.Value.Replace("mail.mycompany.com", 
                                            "devmail.mycompany.com");
}
var query=doc.subjects()
.Element(“SMTPHost”)
.DegeneratNodes()的
.of type();
foreach(查询中的XText textNode)
{
textNode.Value=textNode.Value.Replace(“mail.mycompany.com”,
“devmail.mycompany.com”);
}

非常感谢使用OfType()的示例。。。今天保存了我的培根。非常感谢使用OfType()的示例。。。今天救了我一命。