且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将列添加到要用作XML父节点的数据集

更新时间:2023-11-25 08:20:40

好的,让我们重构一下。

Ok, Let's refactor this.

让我们不要直接从您的数据集中尝试这样做,您正在尝试在这里的方法中做许多事情,难以维护,并且非常难以进行单元测试

Lets not try and do this directly from your dataset, you are trying to do to many things in your method here, it's messy hard to maintain and very hard to unit test.

我们应该做的第一件事是创建一个可以更容易使用的SearchResult类,这也是放置我们的业务规则的便利位置(Ip已添加到用户和随机ActionId),这也意味着我们可以轻松地将数据模拟到这个类中,而不必打到数据库,然后我们可以测试我们的变换逻辑作为单元测试,而不是集成测试(更慢,更多依赖关系)

The first thing we should do is create a SearchResult class that we can work with more easily, this is also a convenient place to put in our Business rules (Ip added to User and random ActionId) it also means that we can easily mock up data into this class without having to hit the database, we can then test our transform logic as a unit test, not an integration test (which are slower, and have more dependencies)

public class SearchResult
{
    public string EventType {get ;set;}
    public string User {get ; set;}
    public DateTime Date {get;set;}
    public int PCBID {get;set;}
    public string Reason {get;set;}

    public string UserAndIP
    {
        get
        {
            return String.Format("{0}:192.168.255.255",User);
        }
    }

    public string ActionId
    {
        get
        {
            return String.Format("{0:X4}", new Random().Next(0xffff));
        }
    }
}

所以让我们重写查询现在填充一个SearchResult的列表而不是数据集

So lets rewrite the query to now populate a list of SearchResult's instead of a dataset

public IEnumerable<SearchResult> GetSearchResults()
{
    using(var conn = GetYourConnection())
    {
        conn.open();
        using(var cmd = conn.CreateCommand())
        {
            cmd.CommandText = GetYourQueryString();
            using(var reader = cmd.ExecuteReader())
            {
                while(reader.Read())
                {
                    var result = new SearchResult
                    {
                        .... populate from reader...
                    }
                    yield return result;
                }
            }           
        }
    }
}

所以现在我们有一个SearchResult类和一个查询方法,给我们一个列表,让它转换成你需要的XML。
首先,我会做出一些假设,从你的问题不是100%清楚。 (如果这些是不正确的,这将很容易修改)

So now that we have a SearchResult class and a query method that gives us a list of them, lets transform that to your required XML. Firstly, I'll make some assumtions that are not 100% clear from your question. (if these are not correct, it will be easy enough to modify)


  1. 我假设我们正在创建一个搜索从我们的查询返回的每个搜索
    结果的结果标签。并且这些将包含在
    PCBDatabaseReply标签中。

  1. I'll assume that we are creating a search result tag for each search result returned from our query. And that these will be contained in the PCBDatabaseReply tag.

xml标签Termination是事件类型的值, ll
假定标签应该是EventType值。

The xml tag "Termination" is the value of the Event Type, so I'll assume that tag should be the EventType value.

让Linq使用XML来创建XML从搜索结果列表中

Lets use Linq to XML to create the XML from the list of SearchResults

首先我们将创建一个方法来转换单个SearchResults(SearchResult标签的内容)

Firstly We'll create a method that transforms individual SearchResults (the contents of the SearchResult tag)

public XElement TransformSearchResult(SearchResult result)
{
    return new XElement("SearchResult",
        new XElement("SBE_PCB_Data", new XAttribute("PCBID", result.PCBID)),
        new XElement(result.EventType,
            new XAttribute("ActionID", result.ActionId),
            new XAttribute("User", result.UserAndIP),
            new XAttribute("Date", result.Date),
            new XAttribute("PCBID", result.PCBID)),
        new XElement("Reason", result.Reason));
}

其次,我们将创建转换列表的方法

Secondly we'll create the method to transform the list

public XElement TransformList(IEnumerable<SearchResult> listOfResults)
{
    return new XElement("PCBDatabaseReply", 
            from r in listOfResults
            select TransformSearchResult(r));
}

现在我们的主调用方法简单地变成...

Now our main calling method simply becomes...

var searchResults = GetSearchResults();
var xml = TransformList(searchResults);