使用反射c#读取XML节点

时间:2017-12-28 15:51:22

标签: c# xml linq system.reflection

我有这个函数来读取xml值并创建一个类的实例。

有没有办法将linq和反射结合起来不指定类的属性并用较少的代码行创建类?

如果我向Test_Class添加或删除一些字段

,我会尽量避免修改此方法
    /// <summary>
    /// Get the values of a xml node and return a class of type Class_Test
    /// </summary>
    /// <param name="sXmlFileName">Path to xml file.</param>
    /// <param name="sNodeName">Name of node to get values.</param>
    /// <returns>Class_Test New class with the values set from XML.</returns>
    public static Class_Test Func_ReadXMLNode(string sXmlFileName, string sNodeName)
    {
        //Load XML
        XDocument xml_Document;
        Class_Test result;
        try
        {
            xml_Document = XDocument.Load(sXmlFileName);
            //Read XML Section
            var xmlValues = from r in xml_Document.Descendants(sNodeName)
                            select new Class_Test
                            {
                                sNetworkInterfaceName = Convert.ToString(r.Element("").Value),
                                iNetworkInterfacePort = Convert.ToInt32(r.Element("").Value),
                                sCertificateFile = Convert.ToString(r.Element("").Value),
                                sCertificateName = Convert.ToString(r.Element("").Value),
                                iKeepAliveSendingTime = Convert.ToInt32(r.Element("").Value),
                                iMaximumTimeWithoutKeepAlive = Convert.ToInt32(r.Element("").Value),
                                sSqlServer = Convert.ToString(r.Element("").Value),
                                sDatabase = Convert.ToString(r.Element("").Value),
                                iFtpRetries = Convert.ToInt32(r.Element("").Value),
                                sDetectionFilesDirectory = Convert.ToString(r.Element("").Value),
                                sImgDirectory = Convert.ToString(r.Element("").Value),
                                sLocalDirectory = Convert.ToString(r.Element("").Value),
                                sOffenceDirectory = Convert.ToString(r.Element("").Value),
                                sTmpDirectory = Convert.ToString(r.Element("").Value)
                            };

            result = xmlValues.FirstOrDefault();

        }
        catch (IOException Exception1)
        {
            LogHelper.Func_WriteEventInLogFile(DateTime.Now.ToLocalTime(), enum_EventTypes.Error, "Func_ReadXMLConfig()", "Source = " + Exception1.Source.Replace("'", "''") + ", Message = " + Exception1.Message.Replace("'", "''"));
            result = new Class_Test();
        }

        return result;
    }

这是Class_Text:

/// <summary>
/// Class of operation mode.
/// </summary>
public class Class_OperationMode
{
    #region CONFIG_PARAMETERS
    /// <summary>
    /// Name of the network interface.
    /// </summary>
    public string sNetworkInterfaceName;
    /// <summary>
    /// Port of the network interface.
    /// </summary>
    public int iNetworkInterfacePort;
    /// <summary>
    /// Path to certificate file.
    /// </summary>
    public string sCertificateFile;
    /// <summary>
    /// Name of the certificate.
    /// </summary>
    public string sCertificateName;
    /// <summary>
    /// Time to keep alive the connection while sending data.
    /// </summary>
    public int iKeepAliveSendingTime;
    /// <summary>
    /// Time before timeout of the connection.
    /// </summary>
    public int iMaximumTimeWithoutKeepAlive;
    /// <summary>
    /// Database server instance.
    /// </summary>
    public string sSqlServer;
    /// <summary>
    /// Path to .mdf file of database.
    /// </summary>
    public string sDatabase;
    /// <summary>
    /// Max retries to try to connect to FTP Server.
    /// </summary>
    public int iFtpRetries;
    /// <summary>
    /// Path to detections files directory.
    /// </summary>
    public string sDetectionFilesDirectory;
    /// <summary>
    /// Path to images directory.
    /// </summary>
    public string sImgDirectory;
    /// <summary>
    /// Path to local directory.
    /// </summary>
    public string sLocalDirectory;
    /// <summary>
    /// Path to folder where save and retrieve offences.
    /// </summary>
    public string sOffenceDirectory;
    /// <summary>
    /// Path to temp directory.
    /// </summary>
    public string sTmpDirectory;

    #endregion

更新:感谢评论,我将代码更新为:

public static Class_Test Func_ReadXMLNode(string sXmlFileName, string sNodeName){
//Load XML
XDocument xml_Document;
Class_Test result;
try
{
    xml_Document = XDocument.Load(sXmlFileName);
    //Read XML Section

    //Get xml values of descendants
    XElement xmlValues = xml_Document.Descendants(sNodeName).FirstOrDefault();
    //Create serializer
    XmlSerializer serializer = new XmlSerializer(typeof(Class_Test));

    //Deserialize 
    using (XmlReader reader = xmlValues.CreateReader())
    {
        result = (Class_Test)serializer.Deserialize(reader);
    }

}
catch (IOException Exception1)
{
    LogHelper.Func_WriteEventInLogFile(DateTime.Now.ToLocalTime(), enum_EventTypes.Error, "Func_ReadXMLConfig()", "Source = " + Exception1.Source.Replace("'", "''") + ", Message = " + Exception1.Message.Replace("'", "''"));
    result = new Class_Test();
}

return result;
}

提前致谢。最好的问候,

华金

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找的是序列化/反序列化。在.net中有许多用于处理xml序列化的有用东西。我已经从文档(下面的链接)中窃取了这个例子。

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

    // A FileStream is needed to read the XML document.
    FileStream fs = new FileStream(filename, FileMode.Open);
    XmlReader reader = XmlReader.Create(fs);

    // Declare an object variable of the type to be deserialized.
    OrderedItem i;

    // Use the Deserialize method to restore the object's state.
    i = (OrderedItem)serializer.Deserialize(reader);
    fs.Close();

来自https://msdn.microsoft.com/en-us/library/tz8csy73(v=vs.110).aspx