Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有多个节点的C#XML解析_C# - Fatal编程技术网

具有多个节点的C#XML解析

具有多个节点的C#XML解析,c#,C#,我有一个格式大致如下的文件: 我要做的是获取Type元素,然后获取每个源的数据并将其输出到文本字段。 似乎无法让它工作` 解析这个的最佳方法是什么 <Config> <Type>8_Port_Switch</Type> ` <Inputs> <Source_1> <Source_1_Name>BobsPC</Source_

我有一个格式大致如下的文件: 我要做的是获取Type元素,然后获取每个源的数据并将其输出到文本字段。 似乎无法让它工作` 解析这个的最佳方法是什么

 <Config>
     <Type>8_Port_Switch</Type>      `
        <Inputs>
              <Source_1>
                <Source_1_Name>BobsPC</Source_1_Name>
                <Source_1_Input>7</Source_1_Input>
              </Source_1>
             <Source_2>
               <Source_2_Name>Office</Source_2_Name>
               <Source_2_Input>4</Source_2_Input>
             </Source_2>
             <Source_3>
              <Source_3_Name>Printer</Source_3_Name>
              <Source_3_Input>3</Source_3_Input>
            </Source_3>
   </Config>

8端口交换机`
BobsPC
7.
办公室
4.
打印机
3.
此文件最多可以有32个端口。我想使用类型信息强制打开特定表单,然后用读取的结果填充表单

每个源都有一个标签,要填充的名称和输入有两个文本框

比如说。我想读取数据并填充:

编辑信息。 然后创建一个新的xml文档并上传到服务器。我可以创造没有问题。它只是读取并将其传递回字段。

尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("Config").Select(x => new {
                name = (string)x.Element("Type"),
                type = x.Element("Inputs").Elements().Select(y => new
                {
                    id = y.Name.LocalName,
                    name = (string)y.Elements().First(),
                    input = (int)y.Elements().Last()
                }).ToList()
            }).ToList();

        }
    }
}
请尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("Config").Select(x => new {
                name = (string)x.Element("Type"),
                type = x.Element("Inputs").Elements().Select(y => new
                {
                    id = y.Name.LocalName,
                    name = (string)y.Elements().First(),
                    input = (int)y.Elements().Last()
                }).ToList()
            }).ToList();

        }
    }
}

选项1

我认为一种干净的方法是将XML的内容放入
数据集中
,然后将表单的控件绑定到表中。下面是一个帮助您的示例:

DataSet ds = new DataSet();
ds.readxml("XML File Path");

var bs = new BindingSource();
bs.DataSource = ds;
bs.DataMember = ds.table[0].tablename;

textBox1.DataBindings.Add("FirstName", bs, "Table Name");
textBox2.DataBindings.Add("FirstName", bs, "Table Name");
选项2

您可以使用我的答案为xml创建一个C#类。然后将xml内容反序列化到C#类中。一旦将其反序列化到C#类中,就可以操作该类的实例。您甚至可以将实例绑定到表单。表单控件可以像任何C#类实例一样更改实例的内容

完成后,就可以序列化回xml。以下是为XML生成的类:

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Config
{

    private string typeField;

    private ConfigInputs inputsField;

    /// <remarks/>
    public string Type
    {
        get
        {
            return this.typeField;
        }
        set
        {
            this.typeField = value;
        }
    }

    /// <remarks/>
    public ConfigInputs Inputs
    {
        get
        {
            return this.inputsField;
        }
        set
        {
            this.inputsField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ConfigInputs
{

    private ConfigInputsSource_1 source_1Field;

    private ConfigInputsSource_2 source_2Field;

    private ConfigInputsSource_3 source_3Field;

    /// <remarks/>
    public ConfigInputsSource_1 Source_1
    {
        get
        {
            return this.source_1Field;
        }
        set
        {
            this.source_1Field = value;
        }
    }

    /// <remarks/>
    public ConfigInputsSource_2 Source_2
    {
        get
        {
            return this.source_2Field;
        }
        set
        {
            this.source_2Field = value;
        }
    }

    /// <remarks/>
    public ConfigInputsSource_3 Source_3
    {
        get
        {
            return this.source_3Field;
        }
        set
        {
            this.source_3Field = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ConfigInputsSource_1
{

    private string source_1_NameField;

    private byte source_1_InputField;

    /// <remarks/>
    public string Source_1_Name
    {
        get
        {
            return this.source_1_NameField;
        }
        set
        {
            this.source_1_NameField = value;
        }
    }

    /// <remarks/>
    public byte Source_1_Input
    {
        get
        {
            return this.source_1_InputField;
        }
        set
        {
            this.source_1_InputField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ConfigInputsSource_2
{

    private string source_2_NameField;

    private byte source_2_InputField;

    /// <remarks/>
    public string Source_2_Name
    {
        get
        {
            return this.source_2_NameField;
        }
        set
        {
            this.source_2_NameField = value;
        }
    }

    /// <remarks/>
    public byte Source_2_Input
    {
        get
        {
            return this.source_2_InputField;
        }
        set
        {
            this.source_2_InputField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ConfigInputsSource_3
{

    private string source_3_NameField;

    private byte source_3_InputField;

    /// <remarks/>
    public string Source_3_Name
    {
        get
        {
            return this.source_3_NameField;
        }
        set
        {
            this.source_3_NameField = value;
        }
    }

    /// <remarks/>
    public byte Source_3_Input
    {
        get
        {
            return this.source_3_InputField;
        }
        set
        {
            this.source_3_InputField = value;
        }
    }
}

选项1

我认为一种干净的方法是将XML的内容放入
数据集中
,然后将表单的控件绑定到表中。下面是一个帮助您的示例:

DataSet ds = new DataSet();
ds.readxml("XML File Path");

var bs = new BindingSource();
bs.DataSource = ds;
bs.DataMember = ds.table[0].tablename;

textBox1.DataBindings.Add("FirstName", bs, "Table Name");
textBox2.DataBindings.Add("FirstName", bs, "Table Name");
选项2

您可以使用我的答案为xml创建一个C#类。然后将xml内容反序列化到C#类中。一旦将其反序列化到C#类中,就可以操作该类的实例。您甚至可以将实例绑定到表单。表单控件可以像任何C#类实例一样更改实例的内容

完成后,就可以序列化回xml。以下是为XML生成的类:

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Config
{

    private string typeField;

    private ConfigInputs inputsField;

    /// <remarks/>
    public string Type
    {
        get
        {
            return this.typeField;
        }
        set
        {
            this.typeField = value;
        }
    }

    /// <remarks/>
    public ConfigInputs Inputs
    {
        get
        {
            return this.inputsField;
        }
        set
        {
            this.inputsField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ConfigInputs
{

    private ConfigInputsSource_1 source_1Field;

    private ConfigInputsSource_2 source_2Field;

    private ConfigInputsSource_3 source_3Field;

    /// <remarks/>
    public ConfigInputsSource_1 Source_1
    {
        get
        {
            return this.source_1Field;
        }
        set
        {
            this.source_1Field = value;
        }
    }

    /// <remarks/>
    public ConfigInputsSource_2 Source_2
    {
        get
        {
            return this.source_2Field;
        }
        set
        {
            this.source_2Field = value;
        }
    }

    /// <remarks/>
    public ConfigInputsSource_3 Source_3
    {
        get
        {
            return this.source_3Field;
        }
        set
        {
            this.source_3Field = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ConfigInputsSource_1
{

    private string source_1_NameField;

    private byte source_1_InputField;

    /// <remarks/>
    public string Source_1_Name
    {
        get
        {
            return this.source_1_NameField;
        }
        set
        {
            this.source_1_NameField = value;
        }
    }

    /// <remarks/>
    public byte Source_1_Input
    {
        get
        {
            return this.source_1_InputField;
        }
        set
        {
            this.source_1_InputField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ConfigInputsSource_2
{

    private string source_2_NameField;

    private byte source_2_InputField;

    /// <remarks/>
    public string Source_2_Name
    {
        get
        {
            return this.source_2_NameField;
        }
        set
        {
            this.source_2_NameField = value;
        }
    }

    /// <remarks/>
    public byte Source_2_Input
    {
        get
        {
            return this.source_2_InputField;
        }
        set
        {
            this.source_2_InputField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ConfigInputsSource_3
{

    private string source_3_NameField;

    private byte source_3_InputField;

    /// <remarks/>
    public string Source_3_Name
    {
        get
        {
            return this.source_3_NameField;
        }
        set
        {
            this.source_3_NameField = value;
        }
    }

    /// <remarks/>
    public byte Source_3_Input
    {
        get
        {
            return this.source_3_InputField;
        }
        set
        {
            this.source_3_InputField = value;
        }
    }
}

真的需要保留它的xml,因为我将编辑文件后读取它,并重新发送到服务器。一切都必须保持XML格式。我从代码的另一部分编写XML并发送。但在这种情况下,我需要更改,我想导入、填充、编辑和重建,然后发送,我真的需要将其保留为xml,因为我将在读取文件后编辑该文件并重新发送到服务器。一切都必须保持XML格式。我从代码的另一部分编写XML并发送。但在这种情况下,我需要改变我想导入,填充,编辑和重建,然后发送