C# 从XML文件中检索值

C# 从XML文件中检索值,c#,asp.net,xml,key-value,C#,Asp.net,Xml,Key Value,我有多个如下所示的XML文件 <?xml version="1.0" encoding="UTF-8"?> <schema> <sp_transaction_id name="sp_transaction_id" value="1" /> <sp_year name="sp_year" value="2015" /> <sp_first_name name="sp_first_name" value="James" /> <sp_

我有多个如下所示的XML文件

<?xml version="1.0" encoding="UTF-8"?>
<schema>
<sp_transaction_id name="sp_transaction_id" value="1" />
<sp_year name="sp_year" value="2015" />
<sp_first_name name="sp_first_name" value="James" />
<sp_gender name="sp_gender" value="Male" />
<sp_date_of_birth name="sp_date_of_birth" value="06-06-1999" />
</schema>

但差不多就是这样(对不起,我是个初学者)。请引导我。感谢您从xml文件中读取数据,您不需要上传它。您可以给出xml的路径并从中读取。您可以使用以下方法从xml中读取

public static XmlDocument LoadXmlDocument(string xmlPath)
    {
        if ((xmlPath == "") || (xmlPath == null) || (!File.Exists(xmlPath)))
            return null;

        StreamReader strreader = new StreamReader(xmlPath);
        string xmlInnerText = strreader.ReadToEnd();
        strreader.Close();

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xmlInnerText);

        return xmlDoc;
    }
对于从xml读取数据,可以使用

XmlDocument xmlDoc = LoadXmlDocument(xmlPath);

XmlNodeList nodes = xmlDoc .SelectNodes("//*");

                foreach (XmlElement node in nodes)
                {
                  .
                  .
                  .
                }

在foreach循环中,您可以获取所需的值,例如sp_year

,用于从xml文件中读取数据,而无需上载该文件。您可以指定xml的路径并从中读取。您可以使用以下方法从xml中读取

public static XmlDocument LoadXmlDocument(string xmlPath)
    {
        if ((xmlPath == "") || (xmlPath == null) || (!File.Exists(xmlPath)))
            return null;

        StreamReader strreader = new StreamReader(xmlPath);
        string xmlInnerText = strreader.ReadToEnd();
        strreader.Close();

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xmlInnerText);

        return xmlDoc;
    }
对于从xml读取数据,可以使用

XmlDocument xmlDoc = LoadXmlDocument(xmlPath);

XmlNodeList nodes = xmlDoc .SelectNodes("//*");

                foreach (XmlElement node in nodes)
                {
                  .
                  .
                  .
                }

在foreach循环中,您可以获得所需的值,例如sp_year

下面的答案显示了如何从中创建XmlDocument。 如果您知道Xml模式是否更改,我建议将其用作用户定义的类。 首先,应该使用XmlAnnotations创建适合Xml文件模式的POCO类。 第二: 具有文件的路径:

XmlSerializer serializer = new XmlSerializer(typeof(definedclass));
using (FileStream fs = File.Open(pathtofile))
using (XmlReader reader = XmlReader.Create(fs))
{
    var xmlObject = serializer.Deserialize(reader); 
}
xmlObject现在是用户定义的类,其值来自xml。 当做
Rafal

下面的答案显示了如何从中创建XmlDocument。 如果您知道Xml模式是否更改,我建议将其用作用户定义的类。 首先,应该使用XmlAnnotations创建适合Xml文件模式的POCO类。 第二: 具有文件的路径:

XmlSerializer serializer = new XmlSerializer(typeof(definedclass));
using (FileStream fs = File.Open(pathtofile))
using (XmlReader reader = XmlReader.Create(fs))
{
    var xmlObject = serializer.Deserialize(reader); 
}
xmlObject现在是用户定义的类,其值来自xml。 当做
Rafal

您可以使用以下代码获取键值对

XDocument doc = XDocument.Load(filePath);
var schemaElement = doc.Element("schema");
foreach (var xElement in schemaElement.Elements())
    {
        Console.WriteLine(xElement.Attribute("name").Value + ":" + xElement.Attribute("value").Value);
    }
Elements方法返回架构元素内的所有元素

不过,如果可能的话,我建议将xml文件更改为这种格式

<?xml version="1.0" encoding="UTF-8"?>
<schema>
<KeyValuePair name="sp_transaction_id" value="1" />
<KeyValuePair name="sp_year" value="2015" />
<KeyValuePair name="sp_first_name" value="James" />
<KeyValuePair name="sp_gender" value="Male" />
<KeyValuePair name="sp_date_of_birth" value="06-06-1999" />
</schema>

您可以使用以下代码获取键值对

XDocument doc = XDocument.Load(filePath);
var schemaElement = doc.Element("schema");
foreach (var xElement in schemaElement.Elements())
    {
        Console.WriteLine(xElement.Attribute("name").Value + ":" + xElement.Attribute("value").Value);
    }
Elements方法返回架构元素内的所有元素

不过,如果可能的话,我建议将xml文件更改为这种格式

<?xml version="1.0" encoding="UTF-8"?>
<schema>
<KeyValuePair name="sp_transaction_id" value="1" />
<KeyValuePair name="sp_year" value="2015" />
<KeyValuePair name="sp_first_name" value="James" />
<KeyValuePair name="sp_gender" value="Male" />
<KeyValuePair name="sp_date_of_birth" value="06-06-1999" />
</schema>

您可以将文件加载到XDocument中,然后使用Linq To XML提取所需信息。下面的示例代码将所有名称/值对加载到类的数组中:

class MyXMLClass
{
  public String FieldName { get; set; }
  public String Value { get; set; }
}
代码获取所有“schema”子体(仅一个,因为它是顶级元素),然后选择&中的所有元素,并为每个提取名称和值的元素创建一个新的类对象

  XDocument xd = XDocument.Load("test.xml");
  MyXMLClass[] xe =
    xd.Descendants("schema")
      .Elements()
      .Select(n => new MyXMLClass {FieldName = n.Attribute("name").Value, Value = n.Attribute("value").Value})
      .ToArray();

您可以将文件加载到XDocument中,然后使用linqtoxml提取所需的信息。下面的示例代码将所有名称/值对加载到类的数组中:

class MyXMLClass
{
  public String FieldName { get; set; }
  public String Value { get; set; }
}
代码获取所有“schema”子体(仅一个,因为它是顶级元素),然后选择&中的所有元素,并为每个提取名称和值的元素创建一个新的类对象

  XDocument xd = XDocument.Load("test.xml");
  MyXMLClass[] xe =
    xd.Descendants("schema")
      .Elements()
      .Select(n => new MyXMLClass {FieldName = n.Attribute("name").Value, Value = n.Attribute("value").Value})
      .ToArray();

谢谢你的帮助,这个答案帮助我解决了我的问题非常感谢你的帮助,这个答案帮助我解决了我的问题非常感谢