Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Get value from a specific tag in XML document

Tags:

c#

.net

xml

I have an xml document which looks like:

<?xml version="1.0" encoding="UTF-8"?>
<cbn:PaidOrderNotification xmlns:cbn="http://xml.test.com/3.12.0.0/test.xsd">
    <cbn:NotificationDate>2016-08-01T07:28:46.679414Z</cbn:NotificationDate>
    <cbn:Purchase cbt:Id="95368158" xmlns:cbt="http://xml.test.com/3.12.0.0/testTypes.xsd">
        <cbt:Status>Test Order</cbt:Status>
        <cbt:Items>
            <cbt:Item cbt:RunningNo="1">
                <cbt:ProductId>178732</cbt:ProductId>
                <cbt:Payment cbt:SubscriptionId="S18767146">
                    <cbt:CancelUrl>https://store.test.com/</cbt:CancelUrl>
                    <cbt:ChangeUrl>https://test.com/</cbt:ChangeUrl>
                </cbt:Payment>
            </cbt:Item>
        </cbt:Items>
        <cbt:ExtraParameters />
    </cbn:Purchase>
</cbn:PaidOrderNotification>

Using C#, I want to get the value inside <cbt:CancelUrl> tag. How can I do that?

like image 466
etrupja Avatar asked Aug 22 '16 11:08

etrupja


1 Answers

First read the xml document like:

var doc = new XmlDocument();
            doc.LoadXml(_xml);

then you could write:

 string CancelUrl = doc.GetElementsByTagName("cbt:CancelUrl")[0].InnerText;
 string ChangeUrl = doc.GetElementsByTagName("cbt:ChangeUrl")[0].InnerText;
like image 182
drgmak Avatar answered Nov 14 '22 17:11

drgmak