Thank for your solution to get node values for each person but I still need a little bit helping to parse each person record into seperated parameters. I actually create a WCF service that take XML string message to break down into seperated node values.
Based on the value parameters to insert them into SQL server database using LINQ to Entity Framework. I am new with WCF and using LINQ to Entity Framework for CRUD, espectially I am try to insert new data from XML message. In service file, I would like to
get:
string Person1Name = item[0].Name;
string Person1DOB = item[0].DOB;
string Person1Salary = item[0].Salary;
string Person1Position = item[0]Position;
string Person2Name = item[1].Name;
string Person2DOB = item[1].DOB;
string Person2Salary = item[1].Salary;
string Person2Position = item[1]Position;
string Person3Name = item[2].Name;
string Person3DOB = item[2].DOB;
string Person3Salary = item[2].Salary;
string Person3Position = item[2]Position;
I try above code but displaying the error not known item??
You cannot use accessor because the value "item" is only IEnumerable instead of List. So please use ToList instead:
XDocument doc = XDocument.Load("XMLFile1.xml");
var result = (from item in doc.Descendants("Name")
select new
{
Name = item.Value,
DOB = item.Parent.Element("DOB").Value,
Salary = item.Parent.Descendants("Salary").First().Value,
Position = item.Parent.Descendants("Position").First().Value
}).ToList();
//Please use result[0]……
Thanks for your solution. That what I was looking for. One more part I need to search for WCF which insert the data into SQL server table based on get XML node values. I know how to use old ADO.NET to insert a record to databases but I would like to update
new ADO.NET Entity Framework. Do you know any article with WCF CRUD service using POCO, repository pattern with entity framework?
avt2k7
Member
290 Points
209 Posts
Get node/element values for XML string!
Jan 31, 2013 01:45 AM|LINK
Hi,<?xml version="1.0" encoding="utf-8" ?> <StaffReport> <Person1> <Name>Robert</Name> <DOB>1-20-2000</DOB> <AdditionalInfo> <Confidential> <Salary>30K</Salary> <Position>Math Teacher</Position> </Confidential> </AdditionalInfo> </Person1> <Person2> <Name>Julie</Name> <DOB>1-20-2007</DOB> <AdditionalInfo> <Confidential> <Salary>25K</Salary> <Position>Science Teacher</Position> </Confidential> </AdditionalInfo> </Person2> <Person3> <Name>Lee</Name> <DOB>1-2-1997</DOB> <AdditionalInfo> <Confidential> <Salary>50K</Salary> <Position>School Principal</Position> </Confidential> </AdditionalInfo> </Person3> </StaffReport> I try to get the node values using DocumentGetByTagName in XML DOMDecker Dong ...
All-Star
118619 Points
18779 Posts
Re: Get node/element values for XML string!
Feb 02, 2013 01:13 AM|LINK
Hi,
You have to use Xdocument's Descedants method to analyze the child node and do the following things to fetch all the values you want:
avt2k7
Member
290 Points
209 Posts
Re: Get node/element values for XML string!
Feb 03, 2013 06:34 AM|LINK
Hi,
Thank for your solution to get node values for each person but I still need a little bit helping to parse each person record into seperated parameters. I actually create a WCF service that take XML string message to break down into seperated node values. Based on the value parameters to insert them into SQL server database using LINQ to Entity Framework. I am new with WCF and using LINQ to Entity Framework for CRUD, espectially I am try to insert new data from XML message. In service file, I would like to get:
string Person1Name = item[0].Name;
string Person1DOB = item[0].DOB;
string Person1Salary = item[0].Salary;
string Person1Position = item[0]Position;
string Person2Name = item[1].Name;
string Person2DOB = item[1].DOB;
string Person2Salary = item[1].Salary;
string Person2Position = item[1]Position;
string Person3Name = item[2].Name;
string Person3DOB = item[2].DOB;
string Person3Salary = item[2].Salary;
string Person3Position = item[2]Position;
I try above code but displaying the error not known item??
=====================================================================
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Get node/element values for XML string!
Feb 03, 2013 06:54 AM|LINK
Hi again,
You cannot use accessor because the value "item" is only IEnumerable instead of List. So please use ToList instead:
XDocument doc = XDocument.Load("XMLFile1.xml"); var result = (from item in doc.Descendants("Name") select new { Name = item.Value, DOB = item.Parent.Element("DOB").Value, Salary = item.Parent.Descendants("Salary").First().Value, Position = item.Parent.Descendants("Position").First().Value }).ToList(); //Please use result[0]……avt2k7
Member
290 Points
209 Posts
Re: Get node/element values for XML string!
Feb 03, 2013 05:45 PM|LINK
Hi,
Thanks for your solution. That what I was looking for. One more part I need to search for WCF which insert the data into SQL server table based on get XML node values. I know how to use old ADO.NET to insert a record to databases but I would like to update new ADO.NET Entity Framework. Do you know any article with WCF CRUD service using POCO, repository pattern with entity framework?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Get node/element values for XML string!
Feb 04, 2013 12:01 AM|LINK
For EF, you can just try to create an edmx file and import all the related tables there.
For WCF, you can ask questions here, where you can get more help:
WCF, ASMX and other Web Services