簡體   English   中英

從XML值獲取適當的變量

[英]Obtain appropriate variable from XML value

我有一些要從XML文件讀取的值。 這些是在程序開始時聲明的,如下所示:

static public int NumRecords;
static public int DBSize;
static public string SourceWorkbookPath;
static public string SourceSheetName;
static public string DestWorkbookPath;
static public string DestSheetName;

然后,我通過執行以下操作來讀取它們的值:

private static void LoadXMLParameters()
    {
        XmlTextReader reader = new XmlTextReader(ParameterFilePath);

        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element: // The node is an element.        
                    reader.MoveToNextAttribute();

                    switch (reader.Value)
                    {
                        case "SourceWorkbookPath":
                            reader.MoveToNextAttribute();
                            SourceWorkbookPath = reader.Value;
                            break;
                        case "SourceSheetName":
                            reader.MoveToNextAttribute();
                            SourceSheetName = reader.Value;
                            break;
                        case "DestWorkbookPath":
                            reader.MoveToNextAttribute();
                            DestWorkbookPath = reader.Value;
                            break;
                        case "DestSheetName":
                            reader.MoveToNextAttribute();
                            DestSheetName = reader.Value;
                            break;
                        case "NumRecords":
                            reader.MoveToNextAttribute();
                            NumRecords = Int32.Parse(reader.Value);
                            break;
                        case "DBSize":
                            reader.MoveToNextAttribute();
                            DBSize = Int32.Parse(reader.Value);
                            break;
                    }
                    break;
            }
        }
    }

有沒有一種方法可以動態地讀取XML參數的值,從而不必為要添加的每個變量添加新的case

當然有可能-但我同意序列化一個類會更好。

我假設在此解決方案中,您正在尋找具有匹配名稱的第一個屬性:

class XmlAttributeParser
{
    IEnumerable<XAttribute> attributes;

    public XmlAttributeParser(string xml)
    {
        attributes = XElement.Parse(xml)
            .DescendantsAndSelf()
            .SelectMany(e => e.Attributes());
    }

    public T GetAttribute<T>(string name)
    {
        return (T)TypeDescriptor.GetConverter(typeof(T))
            .ConvertFromString(attributes.First(a => a.Name == name).Value);
    }
}

用過的:

string xml = "<Root><Foo Bar=\"123\" Baz=\"456\"/></Root>";
XmlAttributeParser parser = new XmlAttributeParser(xml);
int n = parser.GetAttribute<int>("Bar"); // 123

這種方法的缺點是必須將整個文件加載到內存中,並且每次需要查找變量時都必須搜索屬性。

感謝@Ed Plunkett提供的評論,我最終反序列化XML。 首先,我添加了一個類來保存信息:

public class XMLInfo
{
    public int NumRecords;
    public int DBSize;
    public string SourceWorkbookPath;
    public string SourceSheetName;
    public string DestWorkbookPath;
    public string DestSheetName;
}

然后編寫一個方法,將所有信息簡單地轉儲到此類的實例中:

    private static XMLInfo LoadXMLParameters()
    {
        XMLInfo info = new XMLInfo();

        XmlSerializer serializer = new XmlSerializer(typeof(XMLInfo));

        using (Stream reader = new FileStream(ParameterFilePath, FileMode.Open))
        {
            return info = (XMLInfo)serializer.Deserialize(reader);
        }
    }

希望這對使用我最初方法的其他人有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM