EDN Admin
Well-known member
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 cdsdartQualifier 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/>
<cdsdart>SARAH</cdsdart><br/>
<cdsdartType>GIV</cdsdartType><br/>
</cdsd:FirstName><br/>
<cdsd:LastName><br/>
<cdsdart>GOMEZ</cdsdart><br/>
<cdsdartType>FAMC</cdsdartType><br/>
</cdsd:LastName><br/>
<cdsd:OtherName><br/>
<cdsdart>GABRIELA</cdsdart><br/>
<cdsdartType>GIV</cdsdartType><br/>
need to enter :
<cdsdartQualifier>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/cdsdart
string temp = "./defaultemographics/default:Names/cdsd:LegalName/cdsd:OtherName/cdsdart";
string temp1 = "./defaultemographics/default:Names/cdsd:LegalName/cdsd:OtherName/cdsdartType";
string temp2 = "./defaultemographics/default:Names/cdsd:LegalName/cdsd:OtherName";
var query = from XElement xe in d.XPathSelectElements("//emptyatientRecord", 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. <cdsdartQualifier>
Any help would be greatly appreciated, thanks,
<br/>
<br/>
View the full article
I want to add cdsdartQualifier 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/>
<cdsdart>SARAH</cdsdart><br/>
<cdsdartType>GIV</cdsdartType><br/>
</cdsd:FirstName><br/>
<cdsd:LastName><br/>
<cdsdart>GOMEZ</cdsdart><br/>
<cdsdartType>FAMC</cdsdartType><br/>
</cdsd:LastName><br/>
<cdsd:OtherName><br/>
<cdsdart>GABRIELA</cdsdart><br/>
<cdsdartType>GIV</cdsdartType><br/>
need to enter :
<cdsdartQualifier>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/cdsdart
string temp = "./defaultemographics/default:Names/cdsd:LegalName/cdsd:OtherName/cdsdart";
string temp1 = "./defaultemographics/default:Names/cdsd:LegalName/cdsd:OtherName/cdsdartType";
string temp2 = "./defaultemographics/default:Names/cdsd:LegalName/cdsd:OtherName";
var query = from XElement xe in d.XPathSelectElements("//emptyatientRecord", 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. <cdsdartQualifier>
Any help would be greatly appreciated, thanks,
<br/>
<br/>
View the full article