简体   繁体   中英

Obtain appropriate variable from XML value

I have some values that I'm reading in from an XML file. These are declared at the beginning of the program like so:

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

And then I'm reading in their values by doing:

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;
            }
        }
    }

Is there a way in which I can dynamically read in the values of the XML parameters so that I don't need to add a new case for every variable I'd like to add?

Sure it's possible - but I agree that serializing a class would be preferable.

I assume in this solution that you are looking for the first attribute with a matching name:

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);
    }
}

Used:

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

The downsides to this method being that you have to load the entire file into memory and it has to search the attributes each time you want a find a variable.

Thanks to the comment provided by @Ed Plunkett, I ended up deserializing the XML. First, I added a class to hold the information:

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

And then wrote a method which simply dumps all of the information into an instance of this class:

    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);
        }
    }

Hope this helps anyone else who took the same approach as I had initially.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM