使用LINQ从xml读取CDATA不返回预期结果

时间:2013-02-27 03:43:52

标签: c# xml linq linq-to-xml windows-applications

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

1 个答案:

答案 0 :(得分: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();

编辑:可以使用string.TrimRegex.Replace的组合从SQL中删除前导和尾随回车和缩进。

var cleanSql = Regex.Replace(sql.Trim(), @"^\s+", "", RegexOptions.Multiline);