在名称属性中获取具有“标题”作为值的xml标签之间的值?
Posted
技术标签:
【中文标题】在名称属性中获取具有“标题”作为值的xml标签之间的值?【英文标题】:Get value between xml tags having a "title" as value in name attribute? 【发布时间】:2020-12-07 07:32:36 【问题描述】:我目前正在使用包含 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>
我想过滤并获取“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
【问题讨论】:
认为这会有所帮助:***.com/a/18206625/2192663 ? (抱歉,暂时不允许评论) 感谢@Hadzjie 的快速回复。不幸的是,我仍然得到 null 。下面是我对上述文件的代码表示。 //Linq 表达式。XElement item = (from el in webPart.Elements() where (string)el.Attribute("name") == "Title" select el).FirstOrDefault();
//使用 Xpath var test2 = webPart.XPathSelectElement("//webParts/webParts/data/genericWebPartProperties/property[@name='Title']");
【参考方案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());
【讨论】:
非常感谢您的回答。它真的解决了我的问题。但我只需要访问<property name="Title">
的值。但是,我可以使用 dict["title"] 访问它,但是有没有办法只获取它而不是创建字典对象。
区分大小写:dict["Title"]。如果你只需要一个属性而不是 GroupBy 使用 .Where(x => (string)x.Attribute("name") == "Title).Select(x => (string)x).FirstOrDefault() ; 当您执行 Where/Select 时,制作字典同样容易。以上是关于在名称属性中获取具有“标题”作为值的xml标签之间的值?的主要内容,如果未能解决你的问题,请参考以下文章