0

I have an xml file which contains the following tag.

 <ClinicalDocument xmlns:ext="http://ns.electronichealth.net.au/Ci/Cda/Extensions/3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hl7-org:v3">

Now I have to read this and write the attribute value to a string.

Right now I am using the the follwoing code.

XmlDocument xDocument = new XmlDocument();
xDocument.Load(@"c:\Test.xml");
System.Text.StringBuilder ClinicalDocument = new System.Text.StringBuilder();
ClinicalDocument.Append("<ClinicalDocument xmlns:ext=" + xDocument.SelectSingleNode("/ClinicalDocument/@xmlns:ext").Value + " xmlns:xsi=" + xDocument.SelectSingleNode("/ClinicalDocument/@xmlns:xsi").Value + " xmlns=" + xDocument.SelectSingleNode("/ClinicalDocument/@xmlns").Value + ">");
Response.Write(ClinicalDocument);

But its throwing exception "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function. "

Please suggest what am I doing wrong or is there any other alternative. I am completely new to XML.

1 Answer 1

1

What you're trying to get is not an ordinary attribute, it is namespace attribute. You can try using XPath namespace axis to get namespace attribute, something like this :

/*/namespace::ext

working demo example :

var xml = @"<ClinicalDocument xmlns:ext='http://ns.electronichealth.net.au/Ci/Cda/Extensions/3.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='urn:hl7-org:v3'/>";
var doc = new XmlDocument();
doc.LoadXml(xml);
var result = doc.SelectSingleNode("/*/namespace::ext");
Console.WriteLine(result.Value);

Dotnetfiddle Demo

output :

http://ns.electronichealth.net.au/Ci/Cda/Extensions/3.0
Sign up to request clarification or add additional context in comments.

1 Comment

Hey. Thanx. This was exactly what I was looking for.

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.