簡體   English   中英

使用c#將xml元素提取為字符串

[英]Extracting xml element into a string using c#

我有一個 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>

在 Header 中,我想取出“Year”值,這是我一直堅持的方法。 我當前的 C# 最初使用以下內容讀取文件,但我無法從中獲取所需內容,因此可以將其轉換為字符串:

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

這對於您的XDocument來說非常簡單:

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

其中ns應設置為祖先元素(可能是文檔根)中的xmlns="..."屬性中指定的默認命名空間。

我建議閱讀LINQ to XML 指南以獲取有關如何使用 API 的更多說明。

您可以使用以下類將 xml 反序列化為 C# 類對象,然后可以輕松訪問任何屬性以獲得所需的值

驗證碼:

[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);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM