【发布时间】:2016-12-24 21:05:06
【问题描述】:
我正在尝试将 XElement XName 更改为可读格式,但不能。如何用别名替换 XElement 名称?下面是我的 XElement 代码。
var root = new XElement("Reports",
from l in _userReportMasterDataList
join m in _userReportLocalDataList on l.UsrID equals m.TypeValue
select new XElement("Report",
new XElement("CreatedDate", (DateTime)(l.CreatedDate)),
new XElement("LogType", (string)(l.LogType)),
new XElement("LoginID", (string)(l.LoginID)),
new XElement("Name", (string)(m.Name)),
new XElement("AppVersion", (string)(l.AppVersion)),
new XElement("System", (string)(l.System)),
new XElement("UserIP", (string)(l.UserIPAddress)),
new XElement("LoginDate", (DateTime?)(l.LoginDate)),
new XElement("LogoutDate", (DateTime?)(l.LogoutDate)),
new XElement("Remarks", (string)(l.Remarks))), new XElement("Header",
new XElement("ReportID", (string)(r.ReportID)),
new XElement("GroupingColumn1", (string)(r.GroupingColumn1)),
new XElement("PrintedBy", (string)(r.ReqHdr.Server)),
new XElement("PrintedDate", (DateTime)(DateTime.UtcNow))));
在 XName“CreatedDate”的位置,我想用创建日期、“UserIP”替换为“IP 地址”等。
谢谢,
【问题讨论】:
-
XElement 名称遵循 XML 标准。它不能在元素名称上包含空格。但是你可以使用属性来描述元素名称。
-
@CodeNotFound :我如何使用上述代码的属性?你能发布一个例子吗?谢谢!
-
您需要的每个 XElement 构造函数只需一个新的 XAttribute。
-
@CodeNotFound :我做到了,但名称没有改变,而是简单地添加了另一个元素。我想将 XElement 名称更改为最终用户可以理解的带有空格的格式。
-
XML 元素名称不能包含空格。见stackoverflow.com/questions/2519845/…。您可以使用
XmlConvert.EncodeLocalName(String)对带有空格的名称进行编码,然后再对其进行解码。
标签: c# xml linq-to-xml