0
<AspResp errCode="NA" errMsg="NA" status="1" transId="6c8c5901-6119-4c59-89ce-b3f9efb141f2">
    <EResp errCode="NA" errMsg="NA" resCode="ea3229b1-c9ff-455b-8d3f-84a4c2384c85" status="1" ts="2020-04-27T15:00:10.947" txn="90f4f36f-7051-4c6d-bed4-bd717ddfa38d">
        <Signatures>
            <DocSignature error="NA" id="1">test</DocSignature>
        </Signatures>
    </EResp>
</AspResp>

I want value of transId from first node in the above XML.

I used this code but it is useless

foreach (XElement hashElement in doc.Descendants("transId"))
{
    hashValue = (string)hashElement;
}
0

3 Answers 3

1

Select required node. Since transId is an attribute, you should access like this:

string attrtransId  = node.Attributes["transId"].value
Sign up to request clarification or add additional context in comments.

1 Comment

I think this needs a bit more detail to be useful, it doesn't relate to the code in the original question.
0

Take a look at this, It's very detailed & easy to understand.

How do I read and parse an XML file in C#?

string XMLText = @"<AspResp errCode='NA' errMsg='NA' status='1' transId='6c8c5901-6119-4c59-89ce-b3f9efb141f2'><EResp errCode='NA' errMsg='NA' resCode='ea3229b1-c9ff-455b-8d3f-84a4c2384c85' status='1' ts='2020-04-27T15:00:10.947' txn='90f4f36f-7051-4c6d-bed4-bd717ddfa38d'><Signatures><DocSignature error='NA' id='1'>test</DocSignature></Signatures></EResp></AspResp>";
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(XMLText);
        XmlNode node = doc.GetElementsByTagName("AspResp")[0];
        string transId = node.Attributes["transId"].Value;

Comments

0

Tested xpath with http://xpather.com/2S8920tn

string XMLText =
            @"<AspResp errCode='NA' errMsg='NA' status='1' transId='6c8c5901-6119-4c59-89ce-b3f9efb141f2'><EResp errCode='NA' errMsg='NA' resCode='ea3229b1-c9ff-455b-8d3f-84a4c2384c85' status='1' ts='2020-04-27T15:00:10.947' txn='90f4f36f-7051-4c6d-bed4-bd717ddfa38d'><Signatures><DocSignature error='NA' id='1'>test</DocSignature></Signatures></EResp></AspResp>";
var doc = XDocument.Parse(XMLText);
string transId = doc.XPathSelectElement("/node()[1]").Attribute("transId")?.Value;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.