在 xml 标签之间获取具有“title”作为 name 属性值的值?

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

提问人:harish godiyal 提问时间:8/18/2020 更新时间:8/18/2020 访问量:212

问:

我目前正在使用包含 webPartDescription (XML) 集合的文件上传控件导入 Web 部件

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

我想过滤并获取“genericWebPartProperties”中属性元素的值,属性名称为=“Title”,例如:订单标题列表(在上面的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 
                }
C# XML 解析 Linq-to-XML XELEMENT

评论

0赞 Hadzjie 8/18/2020
认为这可能会有所帮助:stackoverflow.com/a/18206625/2192663?(对不起,还不允许发表评论)
0赞 harish godiyal 8/18/2020
感谢@Hadzjie的快速回复。不幸的是,我仍然得到空.下面是我对上述文件的代码表示。Linq 表达式。 使用 XpathXElement item = (from el in webPart.Elements() where (string)el.Attribute("name") == "Title" select el).FirstOrDefault();var test2 = webPart.XPathSelectElement("//webParts/webParts/data/genericWebPartProperties/property[@name='Title']");

答:

0赞 jdweng 8/18/2020 #1

将 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());
 
        }
    }

}

评论

0赞 harish godiyal 8/18/2020
非常感谢您的回答。它真的解决了我的问题。但我只需要访问 的值。但是,我可以使用 dict[“title”] 访问它,但是有没有办法获取它而不是创建字典对象。<property name="Title">
0赞 jdweng 8/18/2020
区分大小写 : dict[“Title”] 。如果你只需要一个属性,那么不要使用 GroupBy .Where(x => (string)x.Attribute(“name”) == “标题).选择(x => (string)x)。FirstOrDefault();当您执行 Where/Select 时,制作字典也同样容易。