我有以下XML,但我很难弄清楚如何使用LINQ to XML直接更新元素:
XElement settings = XElement.Parse (
@"<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
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>");
例如,在上面,我想访问具有placholder值{}
的每个元素并更新文本。在这种情况下,XElement.Parse()
是否正确?我很可能首先将它加载到XDocument
然后更新元素。另外,我如何更新属性,例如Password
上的Type属性?
尝试访问Username,但它说它是InvalidOperationException。
XNamespace xmlns = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace wsa = "http://schemas.xmlsoap.org/ws/2004/08/addressing";
XNamespace wsse = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
XNamespace wsu = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
var textElement = settings.Elements(xmlns + "Envelope").
Elements(xmlns + "Header").
Elements(wsse + "Security").
Elements(wsse + "UsernameToken").
Elements(wsse + "Username").Single();
另外,如果我知道元素的确切位置并且如果我想更新或删除xmlns:例如wsa,那么它只是我或使用XPath看起来更简单吗?如何实现?
答案 0 :(得分:1)
这样做
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
条件,例如
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
个节点的说明来自XElement value in C#
请注意,您需要添加缺少的命名空间:
xmlns:wsse='something' xmlns:wsa='somethingelse' xmlns:wsu='somethingelseelse'
或XElement.Parse
会中断;
如果要更新您知道名称的单个元素:
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,忽略它们的位置。
或者如果您知道节点的确切“路径”,请使用类似
的内容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);
答案 1 :(得分:0)
我还发现我可以使用本地名称而不必通过执行以下操作来声明命名空间:
XElement user = settings.Descendants().Where(o => o.Name.LocalName == "Username").Single();