0

我有一个xml文件如下

<ScriptFileNames>
  <SqlEye>
    <ScriptFile Name='_ws_CommandHistory_AllHistory.sql' Type='SP' SqlEyeAnalysisTime='00:00:01.7817594' FxCopAnalysisTime='00:00:00.2253670' FxCopWarningCount='0' SqlEyeWarningCount='2'>
          <SqlEyeWarnings>
            <SqlEyeWarning message='SD004: Check for existence object then Drop statement before create statement' />
            <SqlEyeWarning message='SP001: Set NoCount statement missing or it should be ON.' />
          </SqlEyeWarnings>
        </ScriptFile>
  </SqlEye>
</ScriptFileNames>

我希望输出是

FileName WarningMessage 格式

例如

_ws_CommandHistory_AllHistory.sql   SD004: Check for existence object then Drop statement before create statement
_ws_CommandHistory_AllHistory.sql   SP001: Set NoCount statement missing or it should be ON.

我的尝试

string input = @"<ScriptFileNames>
  <SqlEye>
    <ScriptFile Name='_ws_CommandHistory_AllHistory.sql' Type='SP' SqlEyeAnalysisTime='00:00:01.7817594' FxCopAnalysisTime='00:00:00.2253670' FxCopWarningCount='0' SqlEyeWarningCount='2'>
          <SqlEyeWarnings>
            <SqlEyeWarning message='SD004: Check for existence object then Drop statement before create statement' />
            <SqlEyeWarning message='SP001: Set NoCount statement missing or it should be ON.' />
          </SqlEyeWarnings>
        </ScriptFile>
  </SqlEye>
</ScriptFileNames>";
XDocument doc = XDocument.Parse(input);
            XElement scriptFileNames = doc.Element("ScriptFileNames");

            var xx = (from x1 in scriptFileNames.Element("SqlEye").Elements("ScriptFile")
                      select new
                          {
                              Name = x1.Attribute("Name").Value                              
                          }).ToList();

我还有我更多的部分

<SqlEyeRemarks>
        <SqlEyeRemark message='SD001: Set QuotedIdentifier ON statement is missing or order mismatch or it should be ON.' />
        <SqlEyeRemark message='SD002: Set AnsiiNullsOn ON statement is missing or order mismatch or it should be ON.' />
        <SqlEyeRemark message='SD009: Missing or order mismatch of Grant statement.' />
      </SqlEyeRemarks>

我怎样才能让他们在一起?

4

2 回答 2

1

您当前的代码仅迭代到该ScriptFile级别。我怀疑最简单的方法是:

var warnings = doc.Descendants("SqlEyeWarning")
                  .Select(x => new {
                      FileName = (string) x.Parent.Parent.Attribute("Name"),
                      Message = (string) x.Attribute("message")
                  })
                  .ToList();

这用于x.Parent.ParentSqlEyeWarning元素过去SqlEyeWarningsScriptFile

我假设这里的所有 SqlEyeWarning元素都在同一种结构中。如果是的话,上面是最简单的。否则,您可以使用:

var warnings = doc.Root
                  .Element("SqlEye")
                  .Elements("ScriptFile")
                  .Elements("SqlEyeWarnings")
                  .Elements("SqlEyeWarning")
                  .Select(x => new {
                      FileName = (string) x.Parent.Parent.Attribute("Name"),
                      Message = (string) x.Attribute("message")
                  })
                  .ToList();
于 2013-06-05T11:37:18.733 回答
0

请参阅下面的代码,它将向控制台写入文件名和相关的警告消息。

这种方法会将您的 XML 反序列化为可供使用的 .NET 对象。

一个小提示 - 尽量避免在 XML 中使用过多的属性。考虑将 ScriptFile 属性转换为元素以增加可读性 =](如果您选择进行更改,则需要修改代码以反映更改 - 将 XmlAttribute 属性更改为 XmlElement)。

祝你好运!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            string input = @"<ScriptFileNames>
                              <SqlEye>
                                <ScriptFile Name='_ws_CommandHistory_AllHistory.sql' Type='SP' SqlEyeAnalysisTime='00:00:01.7817594' FxCopAnalysisTime='00:00:00.2253670' FxCopWarningCount='0' SqlEyeWarningCount='2'>
                                      <SqlEyeWarnings>
                                        <SqlEyeWarning message='SD004: Check for existence object then Drop statement before create statement' />
                                        <SqlEyeWarning message='SP001: Set NoCount statement missing or it should be ON.' />
                                      </SqlEyeWarnings>
                                    </ScriptFile>
                              </SqlEye>
                            </ScriptFileNames>";

            XDocument doc = XDocument.Parse(input);
            XmlSerializer serialiser = new XmlSerializer(typeof(ScriptFileNames));
            ScriptFileNames scriptNames = (ScriptFileNames)serialiser.Deserialize(doc.CreateReader());

            foreach (SqlEyeWarning warning in scriptNames.SqlEye.ScriptFile.SqlEyeWarnings)
            {
                Console.WriteLine(scriptNames.SqlEye.ScriptFile.Name + "\t" + warning.Message);
            }

            Console.ReadLine();
        }

        [XmlRoot]
        public class ScriptFileNames
        {
            [XmlElement("SqlEye")]
            public SqlEye SqlEye { get; set; }
        }

        public class SqlEye
        {
            [XmlElement("ScriptFile")]
            public ScriptFile ScriptFile { get; set; }
        }

        public class ScriptFile
        {
            [XmlArray("SqlEyeWarnings")]
            [XmlArrayItem("SqlEyeWarning", typeof(SqlEyeWarning))]
            public SqlEyeWarning[] SqlEyeWarnings { get; set; }

            [XmlAttribute("Name")]
            public string Name { get; set; }

            [XmlAttribute("Type")]
            public string Type { get; set; }

            [XmlAttribute("SqlEyeAnalysisTime")]
            public string SqlEyeAnalysisTime { get; set; }

            [XmlAttribute("FxCopAnalysisTime")]
            public string FxCopAnalysisTime { get; set; }

            [XmlAttribute("FxCopWarningCount")]
            public string FxCopWarningCount { get; set; }

            [XmlAttribute("SqlEyeWarningCount")]
            public string SqlEyeWarningCount { get; set; }
        }

        public class SqlEyeWarning
        {
            [XmlAttribute("message")]
            public string Message { get; set; }
        }
    }
}
于 2013-06-05T11:41:45.433 回答