有问题阅读/解析Xml文档

时间:2017-10-27 20:42:26

标签: c# xml winforms

我有一个类似的Xml doc(在这里编辑):

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                <section name="WebApptNotificationService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
            </sectionGroup>
        </configSections>
        <connectionStrings>
            removed for questions
        </connectionStrings>
            <startup> 
                    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
            </startup>
        <system.net>
            <mailSettings>
                <smtp deliveryMethod="SpecifiedPickupDirectory">
                    <specifiedPickupDirectory pickupDirectoryLocation="C:\test\"/>      
                </smtp>
            </mailSettings>
         <mailSettings>
                <smtp from="xxxxx@xxxxxx.com">
                    <network host="mail.xxxxxxxx.com" enableSsl="true" password="xxxxxx" port="xxx" userName="xx@xxxxxxxx.com"/>
                </smtp>
                    <smtp from="No.Reply@xxxxxxxxxx.org">
                <network host="1stmsmail.xxxx.local" enableSsl="true" password="xxxxxxx" port="587" userName="ouruser"/>
            </smtp>
        </mailSettings>
        </system.net>
        <applicationSettings>
            <WebApptNotificationService.Properties.Settings>
                <setting name="ConfirmationInterval" serializeAs="String">
                    <value>60000</value>
                </setting>
                <setting name="EmailFromName" serializeAs="String">
                    <value>Test</value>
                </setting>
                <setting name="EmailFromAddress" serializeAs="String">
                    <value>mobile@xxxxx.com</value>
                </setting>
                <setting name="DaysBeforeReminder" serializeAs="String">
                    <value>2</value>
                </setting>
                <setting name="MinTextMinute" serializeAs="String">
                    <value>480</value>
                </setting>
                <setting name="MaxTextMinute" serializeAs="String">
                    <value>1200</value>
                </setting>

            </WebApptNotificationService.Properties.Settings>
        </applicationSettings>
    </configuration>

我正在尝试阅读使用以下代码的值:

    private string parseXML(string name)
        {
            string returnVal = string.Empty;
            XmlDocument xmlDoc = xmlFile.SettingXML;
            //returnVal = xmlDoc.GetElementsByTagName(name).ToString();
            XmlNode node = xmlDoc.SelectSingleNode("//configuration//applicationSettings//WebApptNotificationService.Properties.Settings//setting");
            MessageBox.Show(node.ToString());
            //returnVal = xmlDoc.DocumentElement.Name.(x => x.Element("Author"));
            return returnVal;
        }

       Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim xdoc As XDocument = XDocument.Load(FILENAME)
            Dim versions As IEnumerable(Of XElement) = xdoc.Descendants("Version")
            For Each version As XElement In versions
                For Each trunk As XElement In version.Elements("Trunk")
                    Console.WriteLine(CType(trunk, String))
                    ComboBox1.Items.Add(CType(trunk, String))
                Next trunk

                For Each xEle As XElement In version.Elements("Branch")
                    Console.WriteLine(CType(xEle, String))
                    ComboBox2.Items.Add(CType(xEle, String))
                Next xEle
            Next version

        End Sub

现在xmlFile.SettingXML是一个从配置文件创建的类。它存储为一个类。我希望根据传入的名称从设置名称中获取值。看起来好像我失败了。如果有人可以请我指出处理这个问题的正确方法,我将非常感激。

编辑:如所指出的那样悲惨地失败并没有描述这个问题。现在我在Messagebox中收到System.Xml.XmlElement。

1 个答案:

答案 0 :(得分:0)

正如您所说Right now I am getting System.Xml.XmlElement in the Messagebox尝试使用MessageBox.Show(node.InnerText),这将获得settings元素(节点对象)的内部文本。如果在消息框中显示node对象,它将调用对象whits为System.Xml.XmlElement的ToString方法。

Whit XDocument你可以轻松地做到这一点:

    static void Main(string[] args)
    {
        string value = ParseXML("EmailFromAddress");
        Console.WriteLine(value);
    }

    private static string ParseXML(string name)
    {
        // Load Document use 'XDocument.Parse' if you want to load the document from string
        XDocument xmlDoc = XDocument.Load("C:\\t\\2.txt");

        // Select element setting where name attribute is equal to name
        var node = xmlDoc
            .Descendants("WebApptNotificationService.Properties.Settings")
            .Elements("setting")
            .FirstOrDefault(x => (string) x.Attribute("name") == name);

        return node.Value;
    }