<MyNode>
<default>
<FIELD NAME="AAID" CONTROLTYPE="TextBox" DBFIELD="AA"/>
</default>
<WorkType ID="1">
<FIELD NAME="AAID" CONTROLTYPE="TextBox" DBFIELD="AA"/>
</WorkType>
<WorkType ID="2">
<FIELD NAME="AAID" CONTROLTYPE="TextBox" DBFIELD="AA"/>
</WorkType>
</MyNode>
In above xml i want to read attributes like NAME,CONTROLTYPE etc of FIELD tag.
if WorkType id=1 than read id 1 FIELD
if WorkType id=2 than read id 2 FIELD
if id not match than go to default section and read FIELD attributes.
pls provide linq query code that full fill my requirmrnts.
thanks in advance
XDocument xDocument = XDocument.Load(@"Ur Xml File Path");
var items = from x in xDocument.Descendants("WorkType")
where x.Attribute("ID").Value == "Ur WorkType Id"
select x;
if (items.Count() > 0)
{
var attributes = (from field in items
select new
{
NAME = field.Attribute("NAME").Value,
CONTROLTYPE = field.Attribute("CONTROLTYPE").Value,
DBFIELD = field.Attribute("DBFIELD").Value
}).First();
}
else
{
var attributes = (from def in xDocument.Descendants("FIELD3")
select new
{
NAME = def.Attribute("NAME").Value,
CONTROLTYPE = def.Attribute("CONTROLTYPE").Value,
DBFIELD = def.Attribute("DBFIELD").Value
}).First();
}
XDocument xDocument = XDocument.Load(@"Ur Xml File Path");
var items = from x in xDocument.Descendants("WorkType")
where x.Attribute("ID").Value == "Ur WorkType Id"
select x;
if (items.Count() > 0)
{
var attributes = (from field in items
select new
{
NAME = field.Attribute("NAME").Value,
CONTROLTYPE = field.Attribute("CONTROLTYPE").Value,
DBFIELD = field.Attribute("DBFIELD").Value
}).First();
}
else
{
var attributes = (from def in xDocument.Descendants("FIELD3")
select new
{
NAME = def.Attribute("NAME").Value,
CONTROLTYPE = def.Attribute("CONTROLTYPE").Value,
DBFIELD = def.Attribute("DBFIELD").Value
}).First();
}
info2ambrish
Member
385 Points
275 Posts
How to read xml attribute of following xml using linq?
Apr 11, 2012 01:36 PM|LINK
Hi All
My Xml Is Following:
mm10
Contributor
6395 Points
1182 Posts
Re: How to read xml attribute of following xml using linq?
Apr 11, 2012 01:42 PM|LINK
I don't really understand what you want here, how many/what properties do you wish the return type to have?
info2ambrish
Member
385 Points
275 Posts
Re: How to read xml attribute of following xml using linq?
Apr 11, 2012 01:51 PM|LINK
in my xml
i want conditional linq uery
like
if(work type attribute ID==1)
get the attributes value of FIELD1 tag.
else if(work type attribute ID==2)
get the attributes value of FIELD2 tag.
else
{
in default tag read
get the attributes value of FIELD1 tag.
}
pls provide linq query
Ramesh T
Contributor
5101 Points
827 Posts
Re: How to read xml attribute of following xml using linq?
Apr 11, 2012 02:08 PM|LINK
Try something like this
XDocument xDocument = XDocument.Load(@"Ur Xml File Path"); var items = from x in xDocument.Descendants("WorkType") where x.Attribute("ID").Value == "Ur WorkType Id" select x; if (items.Count() > 0) { var attributes = (from field in items select new { NAME = field.Attribute("NAME").Value, CONTROLTYPE = field.Attribute("CONTROLTYPE").Value, DBFIELD = field.Attribute("DBFIELD").Value }).First(); } else { var attributes = (from def in xDocument.Descendants("FIELD3") select new { NAME = def.Attribute("NAME").Value, CONTROLTYPE = def.Attribute("CONTROLTYPE").Value, DBFIELD = def.Attribute("DBFIELD").Value }).First(); }info2ambrish
Member
385 Points
275 Posts
Re: How to read xml attribute of following xml using linq?
Apr 12, 2012 04:52 AM|LINK
Thanks lot
you save my life.Its working perfectly