0

我正在尝试使用 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;
4

1 回答 1

3

查询的结果是一个IEnumerable实例。如果您想要单个字符串结果,则需要使用FirstOrDefaultSingleOrDefault。此外,在搜索指定的“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();

编辑:前导和尾随回车和缩进可以从 SQL 中剥离 和 的string.Trim组合Regex.Replace

var cleanSql = Regex.Replace(sql.Trim(), @"^\s+", "", RegexOptions.Multiline);
于 2013-02-27T04:08:27.603 回答