Mahatma Aladdin
Mahatma Aladdin

Reputation: 2147

Read node value from input xml file and write it on other output xml file

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.

Upvotes: 0

Views: 120

Answers (1)

har07
har07

Reputation: 89325

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

Upvotes: 1

Related Questions