1

我编写了一个解析 XML 并返回带有标签名称及其值的 Dictionary 的 DLL。我在其他名为 ZennoPoster Project Maker 的程序中使用它。这是代码:

XMLWork.XMLWorker worker = new XMLWork.XMLWorker(); // My parse class
string path = @"Z:\New\test.xml";
Dictionary<string, string> data = worker.GetData(path); // GetData - method, that returns
                                                        // data from XML
project.Variables["second_name"].Value = data["second_name"]; 

这段代码我必须重新制作成 XMLWorker 类方法并返回一个project类型,在 ZennoPoster 中我必须用 1 行代码返回数据。我该怎么做?

4

1 回答 1

1

假设您有以下简化类型:

namespace Objects
{
    public class Project
    {
        public Dictionary<string, Variable> Variables { get; set; }
    }
    public  class Variable
    {
        public object Value { get; set; }
    }
}

您可以XMLWorker像这样构建您的课程:

using Objects;

public class XMLWorker
{
    public Project Project { get; private set; }
    public XMLWorker(string path)
    {
        Project = new Project();
        Dictionary<string, string> data = GetData(path);
        Project.Variables["second_name"].Value = data["second_name"];
    }

    internal Dictionary<string, string> GetData(string path)
    {
        // method implementation
    }
}

示例用法:

var project = (new XMLWork.XMLWorker(@"Z:\New\test.xml")).Project;
于 2013-08-14T13:18:29.860 回答