IrishStout
Programmer
Hi I am somewhat new to working with XML files. I am fine with single namespace requirements but I need to add in a new element in a 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> tag with a value of BR to the existing XML file:
XML file Snippet:
<PatientRecord>
<Demographics>
<Names>
<cdsd:LegalName namePurpose="L">
<cdsd:FirstName>
<cdsdart>SARAH</cdsdart>
<cdsdartType>GIV</cdsdartType>
</cdsd:FirstName>
<cdsd:LastName>
<cdsdart>GOMEZ</cdsdart>
<cdsdartType>FAMC</cdsdartType>
</cdsd:LastName>
<cdsd:OtherName>
<cdsdart>GABRIELA</cdsdart>
<cdsdartType>GIV</cdsdartType>
need to enter: <cdsdartQualifier>BR</PartQualifier>
Here is the code snippet
:
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 BR's Added= " + addCounter.ToString()));
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,
XML file Snippet:
<PatientRecord>
<Demographics>
<Names>
<cdsd:LegalName namePurpose="L">
<cdsd:FirstName>
<cdsdart>SARAH</cdsdart>
<cdsdartType>GIV</cdsdartType>
</cdsd:FirstName>
<cdsd:LastName>
<cdsdart>GOMEZ</cdsdart>
<cdsdartType>FAMC</cdsdartType>
</cdsd:LastName>
<cdsd:OtherName>
<cdsdart>GABRIELA</cdsdart>
<cdsdartType>GIV</cdsdartType>
need to enter: <cdsdartQualifier>BR</PartQualifier>
Here is the code snippet
:
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 BR's Added= " + addCounter.ToString()));
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,