Reputation: 37
I have a string in which I need to extract all of the FieldRef names from the ViewField section of the string. They need to be input into an array. Been struggling with this for a while since im new to c#. Thanks.
<View>
<Query>
<OrderBy>
<FieldRef Name="ID" />
</OrderBy>
</Query>
<ViewFields>
<FieldRef Name="LinkTitle" />
<FieldRef Name="User" />
<FieldRef Name="Permissions" />
</ViewFields>
<RowLimit Paged="TRUE">30</RowLimit>
<JSLink>clienttemplates.js</JSLink>
<XslLink Default="TRUE">main.xsl</XslLink>
<Toolbar Type="Standard"/>
</View>
Upvotes: 2
Views: 96
Reputation: 11228
This returns only FieldRef
s from ViewFields
:
var doc = XDocument.Parse(xmlString);
var results = doc.Root.Element("ViewFields").Elements("FieldRef")
.Select(e => e.Attribute("Name").Value);
Upvotes: 2
Reputation: 1225
Load method is trying to load xml from a file and LoadXml from a string
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
string xpath = "View/Query/ViewFields";
var nodes = xmlDoc.SelectNodes(xpath);
foreach (XmlNode childrenNode in nodes)
{
var arr = childrenNode.SelectSingleNode("//FieldRef").Value;
}
Upvotes: 0
Reputation: 46909
You can use XDocument
to load the string as xml and then use Linq to get the values.
var doc = XDocument.Parse(str);
var res = doc
.Descendants("ViewFields")
.Descendants("FieldRef")
.Select(x => x.Attribute("Name").Value);
Upvotes: 0