c# - Attribute value in root node of xml Linq to XML -
while parsing xml linq xml, came across strange behaviour (atleast me). below first xml parsed
`<?xml version="1.0" encoding="utf-8"?> <testrun> <unittestresult testname = "arun" outcome = "i"> </unittestresult> <unittestresult testname = "arun1" outcome = "i"> </unittestresult> </testrun>`
my code looks
xdocument doc = xdocument.parse(filecontents); var result = doc.descendants("unittestresult");
the above works fine. if root node contain attributes same code not working. reason. xml sample below
<?xml version="1.0" encoding="utf-8"?> <testrun id="7903b4ff-8706-4379-b9e8-567034b70abb" name="inaambika@inbelw013312a 2016-02-26 16:55:14" runuser="stc\inaambika" xmlns="http://microsoft.com/schemas/visualstudio/teamtest/2010"> <unittestresult testname = "arun" outcome = "i"> </unittestresult> <unittestresult testname = "arun1" outcome = "i"> </unittestresult> </testrun> xdocument doc = xdocument.parse(filecontents); var result = doc.descendants("unittestresult");
this 1 below not ordinary attribute, default namespace declaration :
xmlns="http://microsoft.com/schemas/visualstudio/teamtest/2010"
having default namespace declared @ root element level, root element , descendant elements without prefix (in case means elements in posted xml) in namespace.
you can use xnamespace
+ element-local-name reference element in namespace :
xdocument doc = xdocument.parse(filecontents); xnamespace d = "http://microsoft.com/schemas/visualstudio/teamtest/2010" var result = doc.descendants(d+"unittestresult");
Comments
Post a Comment