Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add element with second namespace in XML file.

Status
Not open for further replies.

IrishStout

Programmer
Jun 17, 2011
1
CA
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 <cdsd:partQualifier> tag with a value of BR to the existing XML file:

XML file Snippet:

<PatientRecord>
<Demographics>
<Names>
<cdsd:LegalName namePurpose="L">
<cdsd:FirstName>
<cdsd:part>SARAH</cdsd:part>
<cdsd:partType>GIV</cdsd:partType>
</cdsd:FirstName>
<cdsd:LastName>
<cdsd:part>GOMEZ</cdsd:part>
<cdsd:partType>FAMC</cdsd:partType>
</cdsd:LastName>
<cdsd:OtherName>
<cdsd:part>GABRIELA</cdsd:part>
<cdsd:partType>GIV</cdsd:partType>
need to enter: <cdsd:partQualifier>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/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 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. <cdsd:partQualifier>

Any help would be greatly appreciated, thanks,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top