You'll need to specify the namespace for each element name, not just the first:
doc.Root.Descendants(ns + "Method")
docVersion.Elements(ns + "Parameters")
d.Element(ns + "Parameter").Value
Also, your query will only return the first <Parameter> node under each <Method> node. If you want all of the <Parameter> nodes, you'll need to modify your query:
from docVersion in doc.Root.Descendants(ns + "Method")
from d in docVersion.Elements(ns + "Parameters").Elements(ns + "Parameter")
select new
{
Name = d.Value,
};
Marked as answer by mbanavige on Nov 13, 2012 12:25 AM
niyack
Member
30 Points
11 Posts
parse XML
Nov 11, 2012 10:44 PM|LINK
in the below, I am trying to get the value from the Method node then from the Parameters then from the Parameter.
I am not able to figure out what I am going wrong.
Would someone please assist?
string strGetXML = "<Request AddExpandoFieldTypeSuffix=\"true\" SchemaVersion=\"14.0.0.0\" LibraryVersion=\"14.0.4762.1000\" ApplicationName=\"Silverlight Library\" xmlns=\"http://schemas.microsoft.com/sharepoint/clientquery/2009\"><Actions><ObjectPath Id=\"97\" ObjectPathId=\"96\" /><Query Id=\"98\" ObjectPathId=\"96\"><Query SelectAllProperties=\"true\"><Properties /></Query></Query><ObjectPath Id=\"100\" ObjectPathId=\"99\" /><ObjectPath Id=\"102\" ObjectPathId=\"101\" /><ObjectIdentityQuery Id=\"103\" ObjectPathId=\"101\" /><Query Id=\"104\" ObjectPathId=\"101\"><Query SelectAllProperties=\"true\"><Properties /></Query></Query><ObjectPath Id=\"106\" ObjectPathId=\"105\" /><ObjectIdentityQuery Id=\"107\" ObjectPathId=\"105\" /><ObjectPath Id=\"109\" ObjectPathId=\"108\" /><Method Name=\"SetFieldValue\" Id=\"110\" ObjectPathId=\"108\"><Parameters><Parameter Type=\"String\">Title</Parameter><Parameter Type=\"String\">MDM COP CSS</Parameter></Parameters></Method><Method Name=\"SetFieldValue\" Id=\"111\" ObjectPathId=\"108\"><Parameters><Parameter Type=\"String\">ContainsPII</Parameter><Parameter Type=\"String\">Yes</Parameter></Parameters></Method><Method Name=\"SetFieldValue\" Id=\"112\" ObjectPathId=\"108\"><Parameters><Parameter Type=\"String\">DocumentStatus</Parameter><Parameter Type=\"String\">Draft</Parameter></Parameters></Method><Method Name=\"SetFieldValue\" Id=\"113\" ObjectPathId=\"108\"><Parameters><Parameter Type=\"String\">OriginalDoc</Parameter><Parameter Type=\"String\">Yes</Parameter></Parameters></Method><Method Name=\"SetFieldValue\" Id=\"114\" ObjectPathId=\"108\"><Parameters><Parameter Type=\"String\">SecurityClassification</Parameter><Parameter Type=\"String\">None</Parameter></Parameters></Method><Method Name=\"SetFieldValue\" Id=\"115\" ObjectPathId=\"108\"><Parameters><Parameter Type=\"String\">ExamComponent</Parameter><Parameter Type=\"Array\"><Object TypeId=\"{f1d34cc0-9b50-4a78-be78-d5facfcccfb7}\"><Property Name=\"LookupId\" Type=\"Int32\">1</Property><Property Name=\"LookupValue\" Type=\"Null\" /></Object></Parameter></Parameters></Method><Method Name=\"SetFieldValue\" Id=\"116\" ObjectPathId=\"108\"><Parameters><Parameter Type=\"String\">ExamSubComponent</Parameter><Parameter Type=\"Array\"><Object TypeId=\"{f1d34cc0-9b50-4a78-be78-d5facfcccfb7}\"><Property Name=\"LookupId\" Type=\"Int32\">2</Property><Property Name=\"LookupValue\" Type=\"Null\" /></Object></Parameter></Parameters></Method><Method Name=\"Update\" Id=\"117\" ObjectPathId=\"108\" /><Query Id=\"118\" ObjectPathId=\"108\"><Query SelectAllProperties=\"true\"><Properties><Property Name=\"Title\" ScalarProperty=\"true\" /><Property Name=\"ContainsPII\" ScalarProperty=\"true\" /><Property Name=\"DocumentStatus\" ScalarProperty=\"true\" /><Property Name=\"OriginalDoc\" ScalarProperty=\"true\" /><Property Name=\"SecurityClassification\" ScalarProperty=\"true\" /><Property Name=\"ExamComponent\" ScalarProperty=\"true\" /><Property Name=\"ExamSubComponent\" ScalarProperty=\"true\" /></Properties></Query></Query></Actions><ObjectPaths><Property Id=\"96\" ParentId=\"1\" Name=\"Web\" /><Property Id=\"99\" ParentId=\"96\" Name=\"Lists\" /><Method Id=\"101\" ParentId=\"99\" Name=\"GetByTitle\"><Parameters><Parameter Type=\"String\">Team Files</Parameter></Parameters></Method><Method Id=\"105\" ParentId=\"96\" Name=\"GetFileByServerRelativeUrl\"><Parameters><Parameter Type=\"String\">/204/mike/867095/Team Files/copMain.css</Parameter></Parameters></Method><Property Id=\"108\" ParentId=\"105\" Name=\"ListItemAllFields\" /><StaticProperty Id=\"1\" TypeId=\"{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}\" Name=\"Current\" /></ObjectPaths></Request>";
parseXML(strGetXML);
var q1 = from docVersion in doc.Root.Descendants(ns + "Method")
from d in docVersion.Elements("Parameters")
select new
{
Name = d.Element("Parameter").Value,
};
foreach (var wd in q1)
{
string strGetResults = wd.Name;
MessageBox.Show(strGetResults);
}
RichardD
Contributor
3950 Points
549 Posts
Re: parse XML
Nov 12, 2012 07:01 PM|LINK
You'll need to specify the namespace for each element name, not just the first:
Also, your query will only return the first <Parameter> node under each <Method> node. If you want all of the <Parameter> nodes, you'll need to modify your query:
from docVersion in doc.Root.Descendants(ns + "Method") from d in docVersion.Elements(ns + "Parameters").Elements(ns + "Parameter") select new { Name = d.Value, };niyack
Member
30 Points
11 Posts
Re: parse XML
Nov 12, 2012 07:56 PM|LINK
this worked perfectly, thank you....
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: parse XML
Nov 13, 2012 12:08 AM|LINK
Hello,
I come here to close your issue. Welcome feedback if you have anything urgent by creating another new one.
niyack
Member
30 Points
11 Posts
Re: parse XML
Nov 13, 2012 08:24 PM|LINK
thank you all