Adding new node under a specific parent node - c#

Been studying Linq to XML, and so far I have been able to generate the following XML:
<?xml version="1.0" encoding="utf-8"?>
<_OCROUTPUT xmlns="http://whatever.com/output.xsd">
<_Payer>
<_Name>_Name1</_Name>
<_Address1>_Address11</_Address1>
<_Address2>_Address21</_Address2>
<_City>_City1</_City>
<_State>_State1</_State>
<_ZipCode>_ZipCode1</_ZipCode>
</_Payer>
<_SPANNINGSERVICELINES>
<_ServiceDate>_ServiceDate1</_ServiceDate>
<_CptCode>_CptCode1</_CptCode>
<_Modifier>_Modifier1</_Modifier>
<_ServiceUnits>_ServiceUnits1</_ServiceUnits>
<_ServiceCharges>1</_ServiceCharges>
<_AllowedAmount>1</_AllowedAmount>
<_NonCoveredAmount>_NonCoveredAmount1</_NonCoveredAmount>
<_DeniedAmount>1</_DeniedAmount>
<_ReasonCode>_ReasonCode1</_ReasonCode>
<_PaymentAmount>1</_PaymentAmount>
<_ContractAllowance>1</_ContractAllowance>
<_Sequestration>1</_Sequestration>
<_Deductible>1</_Deductible>
<_Copay>1</_Copay>
<_CoInsurance>1</_CoInsurance>
<_PrimaryPayerPayment>1</_PrimaryPayerPayment>
</_SPANNINGSERVICELINES>
<_PATIENTDETAILS>
<_PatientAccNo>_PatientAccNo1</_PatientAccNo>
<_PatientName>_PatientName1</_PatientName>
<_HicNo>_HicNo1</_HicNo>
<_MedRecNo>_MedRecNo1</_MedRecNo>
<_SERVICELINES>
<_ServiceDate>_ServiceDate1</_ServiceDate>
<_CptCode>_CptCode1</_CptCode>
<_Modifier>_Modifier1</_Modifier>
<_ServiceUnits>_ServiceUnits1</_ServiceUnits>
<_ServiceCharges>1</_ServiceCharges>
<_AllowedAmount>1</_AllowedAmount>
<_NonCoveredAmount>_NonCoveredAmount1</_NonCoveredAmount>
<_DeniedAmount>1</_DeniedAmount>
<_ReasonCode>_ReasonCode1</_ReasonCode>
<_PaymentAmount>1</_PaymentAmount>
<_ContractAllowance>1</_ContractAllowance>
<_Sequestration>1</_Sequestration>
<_Deductible>1</_Deductible>
<_Copay>1</_Copay>
<_CoInsurance>1</_CoInsurance>
<_PrimaryPayerPayment>1</_PrimaryPayerPayment>
</_SERVICELINES>
</_PATIENTDETAILS>
<_PATIENTDETAILS>
<_PatientAccNo>_PatientAccNo2</_PatientAccNo>
<_PatientName>_PatientName2</_PatientName>
<_HicNo>_HicNo2</_HicNo>
<_MedRecNo>_MedRecNo2</_MedRecNo>
<_SERVICELINES>
<_ServiceDate>_ServiceDate4</_ServiceDate>
<_CptCode>_CptCode4</_CptCode>
<_Modifier>_Modifier4</_Modifier>
<_ServiceUnits>_ServiceUnits4</_ServiceUnits>
<_ServiceCharges>0.9</_ServiceCharges>
<_AllowedAmount>0.9</_AllowedAmount>
<_NonCoveredAmount>_NonCoveredAmount4</_NonCoveredAmount>
<_DeniedAmount>0.9</_DeniedAmount>
<_ReasonCode>_ReasonCode4</_ReasonCode>
<_PaymentAmount>0.9</_PaymentAmount>
<_ContractAllowance>0.9</_ContractAllowance>
<_Sequestration>0.9</_Sequestration>
<_Deductible>0.9</_Deductible>
<_Copay>0.9</_Copay>
<_CoInsurance>0.9</_CoInsurance>
<_PrimaryPayerPayment>0.9</_PrimaryPayerPayment>
</_SERVICELINES>
</_PATIENTDETAILS>
<_PATIENTDETAILS>
<_PatientAccNo>_PatientAccNo3</_PatientAccNo>
<_PatientName>_PatientName3</_PatientName>
<_HicNo>_HicNo3</_HicNo>
<_MedRecNo>_MedRecNo3</_MedRecNo>
<_SERVICELINES>
<_ServiceDate>_ServiceDate7</_ServiceDate>
<_CptCode>_CptCode7</_CptCode>
<_Modifier>_Modifier7</_Modifier>
<_ServiceUnits>_ServiceUnits7</_ServiceUnits>
<_ServiceCharges>50.5</_ServiceCharges>
<_AllowedAmount>50.5</_AllowedAmount>
<_NonCoveredAmount>_NonCoveredAmount7</_NonCoveredAmount>
<_DeniedAmount>50.5</_DeniedAmount>
<_ReasonCode>_ReasonCode7</_ReasonCode>
<_PaymentAmount>50.5</_PaymentAmount>
<_ContractAllowance>50.5</_ContractAllowance>
<_Sequestration>50.5</_Sequestration>
<_Deductible>50.5</_Deductible>
<_Copay>50.5</_Copay>
<_CoInsurance>50.5</_CoInsurance>
<_PrimaryPayerPayment>50.5</_PrimaryPayerPayment>
</_SERVICELINES>
</_PATIENTDETAILS>
</_OCROUTPUT>
What I need to do now, is add additional _SERVICELINES under a specific Patient. So for example, if I had another set of data for a new _SERVICELINES, how would I go about say, adding it under _PatientAccNo2?
I'm assuming I need to do a Linq query to isolate _PatientAccNo2, then add the new node and its elements. I just can't seem to figure out how to do that.
This is the query I've come up with (don't yet know if it's correct):
XElement root = XElement.Load(filename)
IEnumerable<XElement> patientDetails =
from patient in root.Elements("_PATIENTDETAILS")
where (string)patient.Element("_PatientName") == "_PatientAccNo2"
select patient;
but after that I'm not sure how to proceed
Thank you in advance!

Try :
XDocument doc = XDocument.Load(FILENAME);
XNamespace ns = doc.Root.GetDefaultNamespace();
XElement patient = doc.Descendants(ns + "_PATIENTDETAILS").ToList().Where(x => (string)x.Element(ns + "_PatientAccNo") == "_PatientAccNo2").FirstOrDefault();
XElement serviceLines = patient.Element(ns + "_SERVICELINES");
serviceLines.Add(new XElement(ns + "New_Element", "123456789"));

Related

XDocument get element that defines it's own namespace

As question states. I have a xml document (below) and I need to get X_ScalarWebApi_DeviceInfo that defines namespace urn:schemas-sony-com:av. Unfortunately it results in an error: {System.NullReferenceException: Object reference not set to an instance of an object. I'm mainly interested in ServiceList element, but it doesn't work as well. Platform - Windows 10 mobile.
Any ideas?
<?xml version="1.0"?>
<root xmlns="urn:schemas-upnp-org:device-1-0">
<specVersion>
<major>1</major>
<minor>0</minor>
</specVersion>
<device>
<deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType>
<friendlyName>ILCE-6000</friendlyName>
<manufacturer>Sony Corporation</manufacturer>
<manufacturerURL>http://www.sony.net/</manufacturerURL>
<modelDescription>SonyDigitalMediaServer</modelDescription>
<modelName>SonyImagingDevice</modelName>
<UDN>uuid:000000001000-1010-8000-62F1894EE7BE</UDN>
<serviceList>
<service>
<serviceType>urn:schemas-sony-com:service:ScalarWebAPI:1</serviceType>
<serviceId>urn:schemas-sony-com:serviceId:ScalarWebAPI</serviceId>
<SCPDURL/>
<controlURL/>
<eventSubURL/>
</service>
</serviceList>
<av:X_ScalarWebAPI_DeviceInfo xmlns:av="urn:schemas-sony-com:av">
<av:X_ScalarWebAPI_Version>1.0</av:X_ScalarWebAPI_Version>
<av:X_ScalarWebAPI_ServiceList>
<av:X_ScalarWebAPI_Service>
<av:X_ScalarWebAPI_ServiceType>guide</av:X_ScalarWebAPI_ServiceType>
<av:X_ScalarWebAPI_ActionList_URL>http://192.168.122.1:8080/sony</av:X_ScalarWebAPI_ActionList_URL>
<av:X_ScalarWebAPI_AccessType/>
</av:X_ScalarWebAPI_Service>
<av:X_ScalarWebAPI_Service>
<av:X_ScalarWebAPI_ServiceType>accessControl</av:X_ScalarWebAPI_ServiceType>
<av:X_ScalarWebAPI_ActionList_URL>http://192.168.122.1:8080/sony</av:X_ScalarWebAPI_ActionList_URL>
<av:X_ScalarWebAPI_AccessType/>
</av:X_ScalarWebAPI_Service>
<av:X_ScalarWebAPI_Service>
<av:X_ScalarWebAPI_ServiceType>camera</av:X_ScalarWebAPI_ServiceType>
<av:X_ScalarWebAPI_ActionList_URL>http://192.168.122.1:8080/sony</av:X_ScalarWebAPI_ActionList_URL>
<av:X_ScalarWebAPI_AccessType/>
</av:X_ScalarWebAPI_Service>
</av:X_ScalarWebAPI_ServiceList>
</av:X_ScalarWebAPI_DeviceInfo>
</device>
</root>
Ah, the code:
XDocument xDoc = XDocument.Parse(xml_text);
//var av = xDoc.Root.GetDefaultNamespace();//.Attribute("xmlns");//
XNamespace av = "urn:schemas-sony-com:av";
System.Diagnostics.Debug.WriteLine(av);
<XElement> api_list = (List<XElement>)xDoc.Element(av + "X_ScalarWebAPI_DeviceInfo").Elements();
==EDIT==
Well, both solutions were ok, so I'm upvoting one and marking as answer the other :P
It was mentioned that the solution using only a 'local name' might cause false positive search results, so to be safe I'm using the first one. Thanks for help!
Element only returns elements directly beneath the current node. You need the to specify the entire path (note there are multiple namespaces):
XNamespace ns = "urn:schemas-upnp-org:device-1-0";
XNamespace av = "urn:schemas-sony-com:av";
var api_list = xDoc.Root.Element(ns + "device")
.Element(av + "X_ScalarWebAPI_DeviceInfo").Elements();
Using Xml Linq
XDocument doc = XDocument.Load(FILENAME);
XElement x_ScalarWebAPI_DeviceInfo = doc.Descendants().Where(x => x.Name.LocalName == "X_ScalarWebAPI_DeviceInfo").FirstOrDefault();
XNamespace ns = x_ScalarWebAPI_DeviceInfo.Name.Namespace;
The issue is indeed the Element or Elements methods only search the direct child elements. You can use Decendants() but you will get a collection, so you have to do First(expression) to het a single one.
But in the expression you can use .Name.LocalName to skip the whole namespace thing and look for just the name of the element.
For this question:
XDocument xDoc = XDocument.Parse(xml_text);
XElement x_ScalarWebAPI_DeviceInfo = doc.Descendants().First(x.Name.LocalName == "X_ScalarWebAPI_DeviceInfo");

Cannot parse xml from Yahoo! Fantasy Sports with c#

I did some searching around the web and could not find the cause of my problem so I apologize if that has already been asked in another form I just did not understand.
My problem is that I am trying to parse the XML retrieved from Yahoo! Fantasy Sports but nothing seems to be working.
I have converted the XML I received (using a GET request with my credentials) into a string. Here it is for evaluation.
<?xml version="1.0" encoding="UTF-8" ?>
- <fantasy_content xml:lang="en-US" yahoo:uri="http://fantasysports.yahooapis.com/fantasy/v2/game/223/players" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" time="5489.1560077667ms" copyright="Data provided by Yahoo! and STATS, LLC" refresh_rate="60" xmlns="http://fantasysports.yahooapis.com/fantasy/v2/base.rng">
- <game>
<game_key>223</game_key>
<game_id>223</game_id>
<name>Football PLUS</name>
<code>pnfl</code>
<type>full</type>
<url>http://football.fantasysports.yahoo.com/f2</url>
<season>2009</season>
- <players count="25">
- <player>
<player_key>223.p.8261</player_key>
<player_id>8261</player_id>
- <name>
<full>Adrian Peterson</full>
<first>Adrian</first>
<last>Peterson</last>
<ascii_first>Adrian</ascii_first>
<ascii_last>Peterson</ascii_last>
</name>
<editorial_player_key>nfl.p.8261</editorial_player_key>
<editorial_team_key>nfl.t.16</editorial_team_key>
<editorial_team_full_name>Minnesota Vikings</editorial_team_full_name>
<editorial_team_abbr>Min</editorial_team_abbr>
- <bye_weeks>
<week>9</week>
</bye_weeks>
<uniform_number>28</uniform_number>
<display_position>RB</display_position>
- <headshot>
<url>http://l.yimg.com/iu/api/res/1.2/7gLeB7TR77HalMeJv.iDVA--/YXBwaWQ9eXZpZGVvO2NoPTg2MDtjcj0xO2N3PTY1OTtkeD0xO2R5PTE7Zmk9dWxjcm9wO2g9NjA7cT0xMDA7dz00Ng--/http://l.yimg.com/j/assets/i/us/sp/v/nfl/players_l/20120913/8261.jpg</url>
<size>small</size>
</headshot>
<image_url>http://l.yimg.com/iu/api/res/1.2/7gLeB7TR77HalMeJv.iDVA--/YXBwaWQ9eXZpZGVvO2NoPTg2MDtjcj0xO2N3PTY1OTtkeD0xO2R5PTE7Zmk9dWxjcm9wO2g9NjA7cT0xMDA7dz00Ng--/http://l.yimg.com/j/assets/i/us/sp/v/nfl/players_l/20120913/8261.jpg</image_url>
<is_undroppable>1</is_undroppable>
<position_type>O</position_type>
- <eligible_positions>
<position>RB</position>
</eligible_positions>
<has_player_notes>1</has_player_notes>
</player>
- <player>
</players>
</game>
</fantasy_content>
The two methods I have tried are these (PLEASE NOTE: "xmlContent" is the string that contains the XML listed above):
1.)
XDocument chicken = XDocument.Parse(xmlContent);
var menus = from menu in chicken.Descendants("name")
select new
{
ID = menu.Element("name").Value,
};
and
2.)
byte[] encodedString = Encoding.UTF8.GetBytes(xmlContent);
MemoryStream ms = new MemoryStream(encodedString);
XmlDocument doc = new XmlDocument();
doc.Load(ms);
foreach (XmlNode row in doc).SelectNodes("//fantasy_content"))
{}
Basically, I get no results enumerated. I have a feeling I am missing some key steps here though. Any help is greatly appreciated. Thank you all.
UPDATE:
As per the awesome suggestions I received, I tried three more things. Since it is not working still, I did not listen very well. :) Actually, that is semi-accurate, please bear with my newbie attempt at working with XML here as I really am thankful for the responses. Here is how I am screwing up the great suggestions, can you offer another tip on what I missed? Thank you all again.
As per Jon Skeet's suggestion (this yields no results for me):
1.) XNamespace ns = "http://fantasysports.yahooapis.com/fantasy/v2/base.rng";
XDocument chicken = XDocument.Parse(xmlContent);
var menus = from menu in chicken.Descendants(ns + "fantasy_content")
select new
{
ID = menu.Element("name").Value,
};
As per the second suggestion (this throws me an error):
2.) var result = XElement.Load(xmlContent).Descendants().Where(x => x.Name.LocalName == "name");
As per the combinations of suggesting I need to identify the namespace and Yahoo! guide at: http://developer.yahoo.com/dotnet/howto-xml_cs.html
3.) xmlContent = oauth.AcquirePublicData(rtUrl, "GET");
byte[] encodedString = Encoding.UTF8.GetBytes(xmlContent);
MemoryStream ms = new MemoryStream(encodedString);
XmlDocument doc = new XmlDocument();
doc.Load(ms);
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("fantasy_content", "http://fantasysports.yahooapis.com/fantasy/v2/base.rng");
XmlNodeList nodes = doc.SelectNodes("/name", ns);
foreach (XmlNode node in nodes)
{
}
This is what's tripping you up:
xmlns="http://fantasysports.yahooapis.com/fantasy/v2/base.rng"
You're asking for elements in the unnamed namespace - but the elements default to the namespace shown above.
It's easy to fix that in LINQ to XML (and feasible but less simple in XmlDocument)
XNamespace ns = "http://fantasysports.yahooapis.com/fantasy/v2/base.rng";
var menus = chicken.Descendants(ns + "name")
...
Note that in your original method you're actually looking for name elements within the name elements - that's not going to work, but the namespace part is probably enough to get you going.
EDIT: It's not clear why you're using an anonymous type at all, but if you really want all the name element values from the document as ID properties in anonymous type instances, just use:
XNamespace ns = "http://fantasysports.yahooapis.com/fantasy/v2/base.rng";
var menus = chicken.Descendants(ns + "name")
.Select(x => new { ID = x.Value });
Note that there's no need to use Descendants(ns + "fantasy_content") as that's just selecting the root element.
Element name consists of two parts: xmlns(namespace) and localname. If xmlns is absent, name is equal to local name. So, you have to create name with namespace or ignore it
You can ignore namespace in your LINQ, just use LocalName
var result = XElement.Load(#"C:\fantasy_content.xml")
.Descendants()
.Where(x => x.Name.LocalName == "name")
.ToList();

Inserting and removing nodes from an XML namespace

I'm trying to replace a node's name but I'm getting the following error "The reference node is not a child of this node". I think I know why this is happening but can't seem to work around this problem. Here is the XML:
<payload:Query1 xmlns="" xmlns:payload="" xmlns:xsi="" xsi:schemaLocation="">
<payload:QueryId>stuff</payload:QueryId>
<payload:Data>more stuff</payload:Data>
</payload:Query1>
And here is the C# bit:
doc.Load(readStream);
nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("payload", "location");
XmlNode Query1 = doc.SelectSingleNode("//payload:Query1", nsmgr);
public XmlDocument sendReply(args)
{
XmlNode newNode = doc.CreateElement("payload:EditedQuery");
Query.InsertBefore(newNode, Query1);
Query.RemoveChild(Query1);
return doc;
}
I'm trying to replace "Query" with "EditedQuery" but his doesn't work.
If you can use .Net 3.5 LINQ to XML,
XElement root = XElement.Load(readStream);
XNamespace ns = "http://somewhere.com";
XElement Query1 = root.Descendants(ns + "Query1").FirstOrDefault();
// should check for null first on Query1...
Query1.ReplaceWith(new XElement(ns + "EditedQuery"));
Or, if you don't know the namespace, or don't want to hard-code it:
XElement root = XElement.Load(readStream);
XElement Query1 = root.Descendants()
.FirstOrDefault(x => x.Name.Localname == "Query1");
// should check for null first on Query1...
Query1.ReplaceWith(new XElement(Query1.Name.Namespace + "EditedQuery"));
See Jon Skeet's reason why to use LINQ to XML here over older API's.

Getting decendant node that contains ":"?

I have loaded a XML into a XDocument that looks something like this :
<MyXML xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MyApp.Client.Main.GUI.Report">
<Wrapper xmlns:d2p1="http://schemas.datacontract.org/2004/07/MyApp.Business.Entity">
<d2p1:_MultipelAttributId>156</d2p1:_MultipelAttributId>
</Wrapper>
</MyXML>
Now I need to get the value of _MultipelAttributId but when stating this :
XElement element = (from cml2 in doc.Descendants("d2p1:_MultipelAttributId") select cml2).FirstOrDefault();
I get exception about the ":"?
So how do I get the valiue?
XNamespace ns = "http://schemas.datacontract.org/2004/07/MyApp.Business.Entity";
var element = xDoc.Descendants(ns + "_MultipelAttributId").FirstOrDefault();
The element name you're querying is not correct. Try this:
XElement element = (
from cml2 in doc.Descendants()
where cml2.Name.LocalName == "_MultipelAttributId"
select cml2).FirstOrDefault();
This MSDN article explains well how to work with namespaces in Linq to XML
http://msdn.microsoft.com/en-us/library/bb669152.aspx
XNamespace d2p1 = "http://schemas.datacontract.org/2004/07/MyApp.Business.Entity";
XElement multipelAttributId = doc.Descendants(d2p1 + "_MultipelAttributId").FirstOrDefault();
Notice that you can seamly create an XNamespace or an XName from a string because Microsoft have writen inplicit casts for these types.

Noob LINQ - reading, filtering XML with XDocument

I'm just learning XDocument and LINQ queries. Here's some simple XML (which doesn't look formatted exactly right in this forum in my browser, but you get the idea . . .)
<?xml version="1.0" encoding="utf-8"?>
<quiz
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.com/name XMLFile2.xsd"
title="MyQuiz1">
<q_a>
<q_a_num>1</q_a_num>
<q_>Here is question 1</q_>
<_a>Here is the answer to 1</_a>
</q_a>
<q_a>
<q_a_num>2</q_a_num>
<q_>Here is question 2</q_>
<_a>Here is the answer to 2</_a>
</q_a>
</quiz>
I can iterate across all elements in my XML file and display their Name, Value, and NodeType in a ListBox like this, no problem:
XDocument doc = XDocument.Load(sPath);
IEnumerable<XElement> elems = doc.Descendants();
IEnumerable<XElement> elem_list = from elem in elems
select elem;
foreach (XElement element in elem_list)
{
String str0 = "Name = " + element.Name.ToString() +
", Value = " + element.Value.ToString() +
", Nodetype = " + element.NodeType.ToString();
System.Windows.Controls.Label strLabel = new System.Windows.Controls.Label();
strLabel.Content = str0;
listBox1.Items.Add(strLabel);
}
...but now I want to add a "where" clause to my query so that I only select elements with a certain name (e.g., "qa") but my element list comes up empty. I tried . . .
IEnumerable<XElement> elem_list = from elem in elems
where elem.Name.ToString() == "qa"
select elem;
Could someone please explain what I'm doing wrong? (and in general are there some good tips for debugging Queries?) Thanks in advance!
The problem is that the Name property is not a string, it's an XName. When you ToString it, you get a lot more than you think.
While it's possible to write the query in the way you're attempting to, also consider these possibilites:
//from nodes immediately below this one
IEnumerable<XElement> elem_list = doc.Elements("qa");
//from nodes of all levels below this node.
IEnumerable<XElement> elem_list = doc.Descendants("qa");
I would perhaps change your query to something that looks more like this
var query = from q_a in document.Descendants("q_a")
select new
{
Number = (int)q_a.Element("q_a_num"),
Question = (string)q_a.Element("q_"),
Answer = (string)q_a.Element("_a")
};
With this, you'll pull from each of your q_a descendants the inner elements into an IEnumerable<[Anonymous Type]>, each object containing the number, question, and answer.
However, if you just want to extract the XElements where the name is q_a, you could do this using a where clause.
IEnumerable<XElement> elem_list = elems.Where(elem => elem.Name.LocalName == "q_a");
Of course, as David B showed, the where clause is not necessary here.
IEnumerable<XElement> elem_list = elems.Elements("q_a");

Categories