我正在尝试使用 LINQ 在 xml 文件中读取“查询”元素值(定义为 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;