且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用LINQ将元素更新为XML?

更新时间:2023-11-07 14:04:28

可以做到

var elements = settings.DescendantNodes().OfType<XText>().Where(p => p.Value.StartsWith("{") && p.Value.EndsWith("}"));

foreach (var node in elements)
{
    // This is an example of manipulation. It will simply remove the {}
    node.Value = node.Value.Substring(1, node.Value.Length - 2);
}

您可以直接在for循环中移动Where条件,例如

You could move the Where condition directly in the for cycle, like

var elements = settings.DescendantNodes().OfType<XText>();

foreach (var node in elements.Where(p => p.Value.StartsWith("{") && p.Value.EndsWith("}")))
{
    // This is an example of manipulation. It will simply remove the {}
    node.Value = node.Value.Substring(1, node.Value.Length - 2);
}

查找XText节点的说明来自 C#中的XElement值

请注意,您需要添加缺少的名称空间:

Note that you'll need to add the missing namespaces:

xmlns:wsse='something' xmlns:wsa='somethingelse' xmlns:wsu='somethingelseelse'

XElement.Parse会断开;

如果您要更新一个名称已知的元素:

If you want to update a single element of which you know the name:

XNamespace wsa = "somethingelse"; // Here you must put the namespace!

var textElement = settings.Descendants(wsa + "MessageID").Single(); // If there is only one Message ID to change (0 or > 1 MessageID will make Single throw), .First() if you are only interested in the first one
textElement.Value = "MessageID"; // Changed!

var textElements = settings.Descendants(wsa + "MessageID") // If there are multipleMessage ID to change

foreach (var textElement in textElements)
{
    textElement.Value = "MessageID"; // Changed!
}

请注意,这些变体将更改第一个/所有wsa:MessageID,而忽略其位置.

Be aware that these variants will change the first/all the wsa:MessageID, ignoring their position.

或者如果您知道节点的确切路径",请使用

or if you know the exact "path" to your node, use something like

var textElements = settings.Elements(name1).Elements(name2)...

,然后使用Single().First()foreach

一小段示例代码:

XElement settings = XElement.Parse(
@"<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:wsa='http://schemas.xmlsoap.org/ws/2004/08/addressing' xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'>
    <soapenv:Header>
        <wsse:Security soapenv:mustUnderstand='1'> 
            <wsse:UsernameToken wsu:Id='UsernameToken-72135529'> 
                <wsse:Username>{USERNAME}</wsse:Username> 
                <wsse:Password Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'>{PASSWORD}</wsse:Password> 
            </wsse:UsernameToken> 
        </wsse:Security> 
        <wsa:MessageID>{MESSAGEID}</wsa:MessageID> 
        <wsa:To>{TO}</wsa:To> 
        <wsa:Action>{ACTION}</wsa:Action> 
        <wsa:From> 
            <wsa:Address>{ADDRESS}</wsa:Address> 
        </wsa:From> 
    </soapenv:Header>
    <soapenv:Body>
        {BODY}
    </soapenv:Body>
</soapenv:Envelope>");


var soapenv = settings.GetNamespaceOfPrefix("soapenv");
var wsa = settings.GetNamespaceOfPrefix("wsa");
var wsse = settings.GetNamespaceOfPrefix("wsse");
var wsu = settings.GetNamespaceOfPrefix("wsu");

XElement username = settings.Elements(soapenv + "Header").Elements(wsse + "Security").Elements(wsse + "UsernameToken").Elements(wsse + "Username").Single();
Console.WriteLine(username);
username.Value = "NewValue";
Console.WriteLine(username);