簡體   English   中英

從C#中的xml文件獲取帶有行號的文本

[英]Get text with line number from xml file in c#

您能告訴我如何從xml文件中獲取行號和文本嗎?

例如...

var varLines = xmldocument.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);

在這里,我有行號,但是如何按行號獲取文本

1.<p>This article contends that a new concept of education
2.</p>
2.<contrib-group>
3.<contrib>Organization, Peabody College of Vanderbil University, 230 Appleton Place,</contrib>
4.@</contrib-group>
5.<day></day>
6.<fpage>3</fpage>

我想要如下所示的輸出:

1.<p>This article contends that a new concept of education
3.<contrib>Organization, Peabody College of Vanderbil University, 230 Appleton Place,</contrib>
4.@</contrib-group>
6.<fpage>3</fpage>

請告訴我如何獲得

我不知道,如果我理解您的問題是對的。
首先,xml不適用於行號,但適用於元素/屬性和內容。 要獲取特定元素,您可以使用XmlReader並轉到您想要閱讀其內容或名稱的每個元素。
參見此處: https : //msdn.microsoft.com/zh-cn/library/aa720458%28v=vs.71%29.aspx

嘗試這個:

首先按照以下步驟拆分文本:

string[] lines = xmldocument.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);

然后,您需要提取內容。

List<string> values = new List<string>(); //Here we save the Values according to your line numbers

注意:提取時,列表將從索引0開始,而行從1 => +1開始!

Regex regex = new Regex(@"<(.*)?>(.*?)</(.*?)");
Match match = regex.Match(line);
values.Add(match.Groups[1].Value);
//Do this inside a foreach-loop for each line.

Output:
values[0]: This article contends that a new concept of education

暫無
暫無

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

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