Reputation: 46591
I have the following XML, but I am having a hard time figuring out how to update the elements directly using 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>");
For example, in the above, I want to access each element that has placholder value {}
and update the text. Is doing XElement.Parse()
correct in this scenario? I am most likely going to load it into and XDocument
first and then update the elements. Also, how could I update attributes, like the Type attribute on Password
?
Trying to get to Username, but it says it is an 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();
Also, is it just me or does using XPath seem simpler if I know the exact position of the elements and if I wanted to update or remove xmlns:wsa for example, how would that be achieved?
Upvotes: 0
Views: 2390
Reputation: 46591
I also found out that I can just use the local names without having to declare the namespaces by doing the following:
XElement user = settings.Descendants().Where(o => o.Name.LocalName == "Username").Single();
Upvotes: 0
Reputation: 111810
This will do it
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);
}
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);
}
The instruction for finding XText
nodes are from XElement value in C#
Note that you'll need to add the missing namespaces:
xmlns:wsse='something' xmlns:wsa='somethingelse' xmlns:wsu='somethingelseelse'
or the XElement.Parse
will break;
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!
or
var textElements = settings.Descendants(wsa + "MessageID") // If there are multipleMessage ID to change
foreach (var textElement in textElements)
{
textElement.Value = "MessageID"; // Changed!
}
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)...
and then use the Single()
, the .First()
or the foreach
A small sample code:
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);
Upvotes: 1