获取 xml 标签之间的值,在名称属性中具有 "title" 作为值?

Get value between xml tags having a "title" as value in name attribute?

我目前正在使用包含 collection of webPartDescription (XML)

的文件上传控件导入 webpart
<wrapper>
  <webParts>
    <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
      <metaData>
        <type src="~/Web/Controls/Test/TestHeaderList.ascx" />
        <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
      </metaData>
      <data>
        <properties>
          <property name="UserCancelled" type="bool">False</property>
          <property name="AllowedRoles" type="string" null="true" />
          <property name="SourceFileName" type="bool">False</property>
          <property name="ViewFilterExpression" type="string" />
          <property name="UserCreated" type="bool">False</property>
          <property name="HideExportButtons" type="bool">False</property>
          <property name="RecordCount" type="bool">False</property>
          <property name="TransferStatus" type="bool">False</property>
          <property name="OrdersHeaderID" type="bool">False</property>
          <property name="TransmitRecordCount" type="bool">False</property>
          <property name="DataSource" type="Lobas.Demo.Web.Controls.Test+Views, Test.Demo.Web, Version=2.2.0.0, Culture=neutral, PublicKeyToken=null">None</property>
          <property name="DeltaCount" type="bool">False</property>
          <property name="OrdersHeaderBatchNo" type="bool">False</property>
          <property name="DateCancelled" type="bool">False</property>
          <property name="ResponseDescription" type="bool">False</property>
          <property name="RunDate" type="bool">False</property>
          <property name="DateCreated" type="bool">False</property>
          <property name="ResponseCode" type="bool">False</property>
        </properties>
        <genericWebPartProperties>
          <property name="Width" type="unit" />
          <property name="Description" type="string" />
          <property name="AllowEdit" type="bool">True</property>
          <property name="ChromeType" type="chrometype">Default</property>
          <property name="TitleIconImageUrl" type="string" />
          <property name="Hidden" type="bool">False</property>
          <property name="TitleUrl" type="string" />
          <property name="AllowClose" type="bool">True</property>
          <property name="AllowMinimize" type="bool">True</property>
          <property name="AllowConnect" type="bool">True</property>
          <property name="Height" type="unit" />
          <property name="HelpMode" type="helpmode">Navigate</property>
          <property name="Title" type="string">Orders Header List</property>
          <property name="CatalogIconImageUrl" type="string" />
          <property name="Direction" type="direction">NotSet</property>
          <property name="AllowZoneChange" type="bool">True</property>
          <property name="ChromeState" type="chromestate">Normal</property>
          <property name="HelpUrl" type="string" />
          <property name="AllowHide" type="bool">True</property>
          <property name="ExportMode" type="exportmode">NonSensitiveData</property>
        </genericWebPartProperties>
      </data>
    </webPart>
  </webParts>
</wrapper>

我想过滤并获取属性名称为“Title”的“genericWebPartProperties”中 属性 元素的值 例如:订单 Header 列表(在上面 xml)

我是 linq 和 xml 的新手。我尝试了几种解决方案,但收到 null

我的代码如下:

//Uploded xml containing multible webparts wraped under <wrapepr> root tag.
XDocument UploadedXmlWebparts = XDocument.Load(e.UploadedFile.FileContent);

// fetching individual webpart xml and storing in a Xelement Collection
                var WebpartCollection = UploadedXmlWebparts.Root.Elements();
                List<ClsImportWp> WebPartDescriptionList = new List<ClsImportWp>();

                foreach (XElement webPart in WebpartCollection)
                {
                   var TitleName = // Get the title tag value here 
                }

使用 xml 带有字典的 linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication166
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement genericWebPartProperties = doc.Descendants().Where(x => x.Name.LocalName == "genericWebPartProperties").FirstOrDefault();
            XNamespace ns = genericWebPartProperties.GetDefaultNamespace();
            Dictionary<string,string> dict = genericWebPartProperties.Elements(ns + "property")
                .GroupBy(x => (string)x.Attribute("name"), y => (string)y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
 
        }
    }

}