1

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.

3
  • does your XML file contains <?xml version="1.0" encoding="utf-8" ?> in the top? Commented Nov 27, 2015 at 10:59
  • Yes it does, but it does not seem to help, when I tried with another simple XML structure, it worked without having <?xml version="1.0" encoding="utf-8" ?> Commented Nov 27, 2015 at 11:03
  • Any error your are getting? Commented Nov 27, 2015 at 11:12

1 Answer 1

1
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.

3
  • 1
    Works perfectly! I began to remove parts of the XML to see what it is that stopped it from working, it was really the <webPart xmlns="schemas.microsoft.com/WebPart/v3"></webPart> element, when i removed this element, everything works fine, and it's only this element that has xmlns definition, anything to consider about that? Commented Nov 27, 2015 at 11:36
  • Sure, I'm not running away :) Commented Nov 27, 2015 at 11:39
  • If you are not run away already, then find my another update. :) Commented Nov 27, 2015 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.