Two Namespaces help required - trying to add a new element to second namespace

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi I am somewhat new to using LinqPad and working with XML files. I am fine with single namespace requirements but I need to add in a new element in the second namespace and I am unable to figure out how. See the area in bold in the code below. Essentially,
I want to add cdsd:PartQualifier with a value of BR to the existing XML file:
XML file Snippet:
<PatientRecord><br/>
<Demographics><br/>
<Names><br/>
<cdsd:LegalName namePurpose="L <br/>
<cdsd:FirstName><br/>
<cdsd:Part>SARAH</cdsd:Part><br/>
<cdsd:PartType>GIV</cdsd:PartType><br/>
</cdsd:FirstName><br/>
<cdsd:LastName><br/>
<cdsd:Part>GOMEZ</cdsd:Part><br/>
<cdsd:PartType>FAMC</cdsd:PartType><br/>
</cdsd:LastName><br/>
<cdsd:OtherName><br/>
<cdsd:Part>GABRIELA</cdsd:Part><br/>
<cdsd:PartType>GIV</cdsd:PartType><br/>
need to enter :
<cdsd:PartQualifier>BR</PartQualifier>
Here is the code snippet
<br/>

<div style="color:Black;background-color:White; <pre>
:
try{
XNamespace ns = "cds";
XDocument d = XDocument.Load(xmlFile);
XmlNamespaceManager r = new XmlNamespaceManager(new NameTable());
r.AddNamespace("empty", ns.ToString());
r.AddNamespace("default", "cds");
r.AddNamespace("cdsd", "cds_dt");

///OmdCds/PatientRecord/Demographics/Names/cdsd:LegalName/cdsd:OtherName/cdsd:Part
string temp = "./default:Demographics/default:Names/cdsd:LegalName/cdsd:OtherName/cdsd:Part";
string temp1 = "./default:Demographics/default:Names/cdsd:LegalName/cdsd:OtherName/cdsd:PartType";
string temp2 = "./default:Demographics/default:Names/cdsd:LegalName/cdsd:OtherName";

var query = from XElement xe in d.XPathSelectElements("//empty:PatientRecord", r)
//where xe.Descendants(demoNameSource).Any()
select xe;

int extCounter = 0;
int addCounter = 0;
foreach (XElement xe in query.ToArray())
{
Console.WriteLine("Start");

try
{
if ((xe.XPathSelectElement(temp, r) != null));
extCounter ++;
{
Console.WriteLine((xe.XPathSelectElement(temp, r).Value));
Console.WriteLine((xe.XPathSelectElement(temp1, r).Value));
XNamespace cdsdNs = XNamespace.Get("cdsd");
xe.XPathSelectElement(temp2, r).Add(new XElement(cdsdNs + "PartQualifier", "BR"));
addCounter ++;
}
}
catch
{
Console.WriteLine("No middle name.");
}

}
Console.WriteLine(string.Concat("Total Number of MiddleNames= " + extCounter.ToString()));
Console.WriteLine(string.Concat("Total Number of MiddleNames BRs Added= " + addCounter.ToString()));
[/code]


When I run the code above I get the following output added in the right place put not the right format:
<PartQualifier xmlns="cdsd BR</PartQualifier>
... which you would think should work but it does not actually have the cdsd: in front of PartQualifier i.e. <cdsd:PartQualifier>
Any help would be greatly appreciated, thanks,

<br/>
<br/>


View the full article
 
Back
Top