使用LINQ从xml读取CDATA,但未返回预期结果
本文关键字:返回 结果 LINQ xml 读取 CDATA 使用 | 更新日期: 2023-09-27 18:01:14
我正在尝试使用LINQ读取xml文件中的"query"元素值(定义为CDATA(,但是,当我运行应用程序时,会返回以下值:
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String]
我很困惑我错过了什么来获得CDATA值。
XML文档如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<dbprocedurestruct>
<variables id="vars">
<variable id="ooid">
<name>OrigOrgID</name>
<pseudo>Original Organization ID</pseudo>
<type>System.Int32</type>
<visible>true</visible>
</variable>
<variable id="noid">
<name>NewOrgID</name>
<pseudo>New Organization(s) ID</pseudo>
<type>System.Int32</type>
<visible>true</visible>
</variable>
<variable id="nssid">
<name>NewSysSubID</name>
<pseudo>New System Subscription ID</pseudo>
<type>System.Int32</type>
<visible>false</visible>
</variable>
</variables>
<scripts>
<script name="createscript">
<query>
<![CDATA[
SELECT
SystemSubscriptionID,
'INSERT INTO dbo.Subscriptions
(
SubscriptionID,
SystemSubscriptionTierID,
IsCustomized,
SubscriptionPrice,
DownloadPrice,
PlansPrice,
SpecsPrice,
SubscriptionStartDate,
SubscriptionDuration,
AutoRenewSubscription,
IsActive,
SubscriptionStatus,
CreatedBy,
CreatedOn,
LastEditBy,
LastEditOn)
VALUES (' +
CAST(SubscriptionID AS VARCHAR(50)) + ',' +
CAST(SystemSubscriptionTierID AS VARCHAR(50)) + ',' +
CAST(IsCustomized AS VARCHAR(50)) + ',' +
CAST(SubscriptionPrice AS VARCHAR(50)) + ',' +
CAST(DownloadPrice AS VARCHAR(50)) + ',' +
CAST(PlansPrice AS VARCHAR(50)) + ',' +
CAST(SpecsPrice AS VARCHAR(50)) + ',''' +
CAST(GetDate() AS VARCHAR(50)) + ''',' +
CAST(SubscriptionDuration AS VARCHAR(50)) + ',' +
CAST(AutoRenewSubscription AS VARCHAR(50)) + ',' +
CAST(IsActive AS VARCHAR(50)) + ',' +
CAST(SubscriptionStatus AS VARCHAR(50)) + ', 2, ''' +
CAST(GetDate() AS VARCHAR(50)) + ''', 2, ''' +
CAST(GetDate() AS VARCHAR(50)) + ''') '
FROM Subscriptions WHERE SystemSubscriptionID IN
(SELECT SystemSubscriptionID FROM SubscriptionMappings WHERE ReferenceID = {0});
SELECT IDENT_CURRENT('Subscriptions')
]]>
</query>
</script>
<script name="mappingscript">
<query>
<![CDATA[
INSERT INTO SubscriptionMappings
(SystemSubscriptionID,
ReferenceID,
SubscriptionTypeID,
SystemId,
IsActive,
CreatedBy)
SELECT
{0},
{1},
CAST(SubscriptionTypeID AS VARCHAR(50)),
2,
CAST(IsActive AS VARCHAR(50)),
2 FROM SubscriptionMappings
WHERE ReferenceID = {2}
AND
SystemSubscriptionID = {3}
]]>
</query>
</script>
</scripts>
</dbprocedurestruct>
以下是我读取xml文件的代码片段:
SQLConnectivity _sc = new SQLConnectivity();
DataTable dt = new DataTable();
string xmlitem = (from scripts in xfile.Descendants("scripts") where scripts.Element("script").Value == "createscript" select scripts.Element("script").Element("query").Value).ToString();
//String tempquery = xmlitem.Elements("query").ToString();
String finalquery = String.Format(xmlitem, _ooid);
_sc.RunExecuteSQL(finalquery, ref dt);
return dt;
查询的结果是一个IEnumerable
实例。如果您想要单个字符串结果,则需要使用FirstOrDefault
或SingleOrDefault
。此外,在搜索指定的"createscript"元素时,您需要使用"name"属性。
var sql = (from scripts in xfile.Root.Elements("scripts")
from script in scripts.Elements("script")
let name = script.Attribute("name")
where name != null && name.Value == "createscript"
from query in script.Elements("query")
select query.Value).FirstOrDefault();
编辑:使用string.Trim
和Regex.Replace
组合,可以从SQL中剥离前导和尾随回车和缩进。
var cleanSql = Regex.Replace(sql.Trim(), @"^'s+", "", RegexOptions.Multiline);