C# 从XML获取值

C# 从XML获取值,c#,xml,C#,Xml,我有一个XML文件,其中填充的值如下所示: <property name ="Web Application" value="\Fxnet2\Web\webpl\" /> <property name="Web Service" value="\FXnet2\Web\FXnet_SC_WS\" /> 对于每一行,我想将名称导入一个字符串(我们称之为serviceName),并将值导入另一个字符串(我们称之为servicePath) 我在xml中得到了大约250行这样

我有一个XML文件,其中填充的值如下所示:

<property name ="Web Application" value="\Fxnet2\Web\webpl\" />
<property name="Web Service" value="\FXnet2\Web\FXnet_SC_WS\" />

对于每一行,我想将名称导入一个字符串(我们称之为serviceName),并将值导入另一个字符串(我们称之为servicePath)

我在xml中得到了大约250行这样的内容,可以用当前的xml格式吗?如果是这样的话,怎么办,或者我应该更改列表的格式


提前谢谢

您可以在循环中获取所有节点并访问它们的属性。在下面的示例中,我将两个属性的值添加到两个不同的数组中,您可以在以后轻松地对其进行操作

XmlDocument doc = new XmlDocument();
doc.Load("yourfile.xml");
XmlNodeList usernodes = doc.SelectNodes("//property");

//Declare arrays
List<string> serviceName = new List<string>();
List<string> servicePath = new List<string>();

//iterate through all elements found
foreach (XmlNode usernode in usernodes)
{
    serviceName.Add(usernode.Attributes["name"].Value);
    serviceName.Add(usernode.Attributes["value"].Value);
}
XmlDocument doc=新的XmlDocument();
Load(“yourfile.xml”);
XmlNodeList usernodes=doc.SelectNodes(“//属性”);
//声明数组
List serviceName=新列表();
列表服务路径=新列表();
//遍历找到的所有元素
foreach(usernodes中的XmlNode usernode)
{
serviceName.Add(usernode.Attributes[“name”].Value);
serviceName.Add(usernode.Attributes[“value”].value);
}

您可以在循环中获取所有节点并访问它们的属性。在下面的示例中,我将两个属性的值添加到两个不同的数组中,您可以在以后轻松地对其进行操作

XmlDocument doc = new XmlDocument();
doc.Load("yourfile.xml");
XmlNodeList usernodes = doc.SelectNodes("//property");

//Declare arrays
List<string> serviceName = new List<string>();
List<string> servicePath = new List<string>();

//iterate through all elements found
foreach (XmlNode usernode in usernodes)
{
    serviceName.Add(usernode.Attributes["name"].Value);
    serviceName.Add(usernode.Attributes["value"].Value);
}
XmlDocument doc=新的XmlDocument();
Load(“yourfile.xml”);
XmlNodeList usernodes=doc.SelectNodes(“//属性”);
//声明数组
List serviceName=新列表();
列表服务路径=新列表();
//遍历找到的所有元素
foreach(usernodes中的XmlNode usernode)
{
serviceName.Add(usernode.Attributes[“name”].Value);
serviceName.Add(usernode.Attributes[“value”].value);
}

终于成功了

static void Main(string[] args)
        {
            List<Service> services;

            using(StreamReader file = File.OpenText("c:\\projects.xml"))
            {

                XDocument doc = XDocument.Load(file);
                services = (from node in doc.Root.Elements("property")
                               select new Service
                               {
                                   serviceName = node.Attribute("name").Value,
                                   servicePath = node.Attribute("value").Value,
                                   dllFiles = System.IO.Directory.GetFiles( "servicePath", "*.dll" ),
                               }).ToList<Service>();

        }
static void Main(字符串[]args)
{
列出服务;
使用(StreamReader file=file.OpenText(“c:\\projects.xml”))
{
XDocument doc=XDocument.Load(文件);
服务=(来自doc.Root.Elements(“属性”)中的节点)
选择新服务
{
serviceName=node.Attribute(“name”).Value,
servicePath=node.Attribute(“value”).value,
dllFiles=System.IO.Directory.GetFiles(“servicePath”,“*.dll”),
}).ToList();
}

终于成功了

static void Main(string[] args)
        {
            List<Service> services;

            using(StreamReader file = File.OpenText("c:\\projects.xml"))
            {

                XDocument doc = XDocument.Load(file);
                services = (from node in doc.Root.Elements("property")
                               select new Service
                               {
                                   serviceName = node.Attribute("name").Value,
                                   servicePath = node.Attribute("value").Value,
                                   dllFiles = System.IO.Directory.GetFiles( "servicePath", "*.dll" ),
                               }).ToList<Service>();

        }
static void Main(字符串[]args)
{
列出服务;
使用(StreamReader file=file.OpenText(“c:\\projects.xml”))
{
XDocument doc=XDocument.Load(文件);
服务=(来自doc.Root.Elements(“属性”)中的节点)
选择新服务
{
serviceName=node.Attribute(“name”).Value,
servicePath=node.Attribute(“value”).value,
dllFiles=System.IO.Directory.GetFiles(“servicePath”,“*.dll”),
}).ToList();
}
使用XML Linq

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                "<Root>" +
                    "<property name =\"Web Application\" value=\"\\Fxnet2\\Web\\webpl\" />" +
                    "<property name=\"Web Service\" value=\"\\FXnet2\\Web\\FXnet_SC_WS\" />" +
                "</Root>";

            XElement root = XElement.Parse(input);

            var results = root.Descendants("property").Select(x => new {
                name = x.Attribute("name").Value,
                value = x.Attribute("value").Value
            }).ToList();

        }
    }
}
​
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串输入=
"" +
"" +
"" +
"";
XElement root=XElement.Parse(输入);
var results=root.subjects(“属性”)。选择(x=>new{
name=x.Attribute(“name”).Value,
值=x.属性(“值”).值
}).ToList();
}
}
}
​
使用XML Linq

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                "<Root>" +
                    "<property name =\"Web Application\" value=\"\\Fxnet2\\Web\\webpl\" />" +
                    "<property name=\"Web Service\" value=\"\\FXnet2\\Web\\FXnet_SC_WS\" />" +
                "</Root>";

            XElement root = XElement.Parse(input);

            var results = root.Descendants("property").Select(x => new {
                name = x.Attribute("name").Value,
                value = x.Attribute("value").Value
            }).ToList();

        }
    }
}
​
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串输入=
"" +
"" +
"" +
"";
XElement root=XElement.Parse(输入);
var results=root.subjects(“属性”)。选择(x=>new{
name=x.Attribute(“name”).Value,
值=x.属性(“值”).值
}).ToList();
}
}
}
​

是的,在您当前的XML格式中这样做非常简单。我建议使用LINQ to XML-您可以轻松地将该格式的文档转换为名称/值对列表。我建议您尝试一下,然后在遇到实际问题时编辑您的问题。另一种可能是创建数据类型
属性
和将文件反序列化为一个
属性数组
,然后访问代码中的数据。我正在考虑创建一个名为project的类,该类包含包含名称和值的变量,并为每一行创建一个新对象,抱歉,如果我的问题看起来很愚蠢,我是C#新手programming@user3521610是的,你能做到,德西rialize您的xml to List,或者您也可以使用运行在元素()上的linq to xml高效地读取它xDocument的名称。@JonSkeet的大名誉卡在SO中产生了格式问题:)是的,在您当前的XML格式中这样做非常简单。我建议使用LINQ to XML-您可以轻松地将该格式的文档转换为名称/值对列表。我建议您尝试一下,然后在遇到实际pr时编辑您的问题问题。另一种可能是创建一个数据类型
属性
,并将文件反序列化为一个
属性
数组,然后访问代码中的数据。我正在考虑创建一个名为project的类,其中包含包含名称和值的变量,并为每一行创建一个新对象,如果我有问题,很抱歉看起来很蠢,我是C#的新手programming@user3521610是的,你能做到,而且