Reading addIn XML using Xml.Linq
-
06-10-2020 - |
Frage
I have seen many instances of this question in this forum, with answers! So why am I still asking?
Because I have tried for hours to read specific parts of the XML below using XDocument
and yet no succees, here is the whole addIn XML as exported from a page:
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="Microsoft.SharePoint.WebPartPages.ClientWebPart, Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
<importErrorMessage>Cannot import this Web Part.</importErrorMessage>
</metaData>
<data>
<properties>
<property name="TitleIconImageUrl" type="string" />
<property name="Direction" type="direction">NotSet</property>
<property name="ExportMode" type="exportmode">All</property>
<property name="HelpUrl" type="string" />
<property name="Hidden" type="bool">False</property>
<property name="Description" type="string">Description</property>
<property name="FeatureId" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">624a6c65-81ec-4670-8204-f1e2162e98c0</property>
<property name="CatalogIconImageUrl" type="string" />
<property name="Title" type="string">Title</property>
<property name="AllowHide" type="bool">True</property>
<property name="ProductWebId" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">936f3da1-4a4f-46af-b0f8-fbbcb1556430</property>
<property name="AllowZoneChange" type="bool">True</property>
<property name="TitleUrl" type="string" />
<property name="ChromeType" type="chrometype">TitleOnly</property>
<property name="AllowConnect" type="bool">True</property>
<property name="Width" type="unit">350px</property>
<property name="Height" type="unit" />
<property name="WebPartName" type="string">WebPart</property>
<property name="HelpMode" type="helpmode">Navigate</property>
<property name="AllowEdit" type="bool">True</property>
<property name="AllowMinimize" type="bool">True</property>
<property name="ProductId" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">624a6c65-81ec-4670-8204-f1e2162e98bf</property>
<property name="AllowClose" type="bool">True</property>
<property name="ChromeState" type="chromestate">Normal</property>
</properties>
</data>
</webPart>
</webParts>
I can load the XML using:
XDocument addinTemplate = XDocument.Load("AddInTemplate.xml");
with no problems, but when I try to reach any other part of the XML like the , it just doesn't work.
Shouldn't it be as simple as
var properties = addinTemplate.Descendants("properties");
?
What am I missing?
I have also tried to create my own simple XML structure and had no problems getting any specific element/attribute from it, what is different with addIn XML?
My goal is to reach the properties element to loop through its property children.
Update
Using:
var properties = addinTemplate.Descendants("properties").Select(p => new
{
name = p.Attribute("name"),
type = p.Attribute("type")
}).ToList();
Or:
var properties = addinTemplate.Descendants("properties");
does not yield any results, without getting an error message.
Lösung
var properties = addinTemplate.Descendants("properties")
.Select(p => new {
name = p.Attribute("name"),
type = p.Attribute("type")}
).ToList();
Instead of above, try following and let me know.
var properties = addinTemplate
.Descendants()
.Where(e => e.Name.LocalName == "properties")
.ToList();
Update #1
Following will give all Descendants
under properties
var properties = addinTemplate.Descendants()
.Where(e => e.Name.LocalName == "properties")
.Descendants()
.Select(p => new { name = p.Attribute("name"), type = p.Attribute("type") })
.ToList();
I have tested it.