使用C#将xml元素提取到字符串中

时间:2019-02-20 12:00:45

标签: c# xml

根据下面的示例,我有一个xml文件:

  <Header>
<CollectionDetails>
  <Collection></Collection>
  <Year>1415</Year>
  <FilePreparationDate></FilePreparationDate>
</CollectionDetails>
<Source>
  <ProtectiveMarking>PROTECT-PRIVATE</ProtectiveMarking>
  <UKPRN></UKPRN>
  <TransmissionType>A</TransmissionType>
  <SoftwareSupplier></SoftwareSupplier>
  <SoftwarePackage></SoftwarePackage>
  <Release>12.0.2.3</Release>
  <SerialNo>01</SerialNo>
  <DateTime>2015-10-22T17:01:51.800</DateTime>
</Source>

在标题内,我想取出“年”值,这是我坚持要做的事情。我当前的C#首先使用下面的代码读取文件,但是我无法从文件中获取所需的信息,因此可以将其转换为字符串:

            XElement document = XElement.Load(str_FileLocation);
            var year = document.Element("Header");
            var year1 = year.Elements("CollectionDetails");
            var year2 = year1.Nodes("Year");

2 个答案:

答案 0 :(得分:1)

您的XDocument的确非常简单:

XNamespace ns = "<default namespace here>"
var value = (string) doc.Descendants(ns + "Year").Single()

应将ns设置为祖先元素(可能是文档根目录)的xmlns="..."属性中指定的默认名称空间。

我建议阅读the LINQ to XML guide,以获取有关如何使用API​​的更多说明。

答案 1 :(得分:-1)

您可以使用以下类将xml反序列化为C#类对象,然后轻松访问任何属性以获取所需的值

POC代码:

[XmlRoot(ElementName="CollectionDetails")]
public class CollectionDetails {
    [XmlElement(ElementName="Collection")]
    public string Collection { get; set; }
    [XmlElement(ElementName="Year")]
    public string Year { get; set; }
    [XmlElement(ElementName="FilePreparationDate")]
    public string FilePreparationDate { get; set; }
}

[XmlRoot(ElementName="Source")]
public class Source {
    [XmlElement(ElementName="ProtectiveMarking")]
    public string ProtectiveMarking { get; set; }
    [XmlElement(ElementName="UKPRN")]
    public string UKPRN { get; set; }
    [XmlElement(ElementName="TransmissionType")]
    public string TransmissionType { get; set; }
    [XmlElement(ElementName="SoftwareSupplier")]
    public string SoftwareSupplier { get; set; }
    [XmlElement(ElementName="SoftwarePackage")]
    public string SoftwarePackage { get; set; }
    [XmlElement(ElementName="Release")]
    public string Release { get; set; }
    [XmlElement(ElementName="SerialNo")]
    public string SerialNo { get; set; }
    [XmlElement(ElementName="DateTime")]
    public string DateTime { get; set; }
}

[XmlRoot(ElementName="Header")]
public class Header {
    [XmlElement(ElementName="CollectionDetails")]
    public CollectionDetails CollectionDetails { get; set; }
    [XmlElement(ElementName="Source")]
    public Source Source { get; set; }
}

示例代码:

XmlSerializer serializer = new XmlSerializer(typeof(Header));
StreamReader reader = new StreamReader(path);
var header = (Header)serializer.Deserialize(reader);
reader.Close();
//use the header object to access your data



更新 这是使用xpath的另一种方法:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.XPath;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            XPathNavigator nav;
            XPathDocument docNav;
            string xPath;

            docNav = new XPathDocument(AppDomain.CurrentDomain.BaseDirectory + "test.xml");
            nav = docNav.CreateNavigator();
            xPath = "/Header/CollectionDetails/Year/text()";

            string value = nav.SelectSingleNode(xPath).Value;
            Console.WriteLine(value);
        }
    }
}