[英]Get text with line number from xml file in c#
Can you please tell me how to get line number and text from xml file? 您能告诉我如何从xml文件中获取行号和文本吗?
for example... 例如...
var varLines = xmldocument.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
Here, I got line numbers but how to get text as per line number 在这里,我有行号,但是如何按行号获取文本
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>
I want to have the ouptut as shown below: 我想要如下所示的输出:
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>
Please tell me how to get it 请告诉我如何获得
I don´t know, if I understand your question right. 我不知道,如果我理解您的问题是对的。
First of all xml doesn´t work with line numbers, but with elements/attributes and content. 首先,xml不适用于行号,但适用于元素/属性和内容。 To get specific elements you could jut use the XmlReader and go to each element you like to read it´s content or it´s name.
要获取特定元素,您可以使用XmlReader并转到您想要阅读其内容或名称的每个元素。
See here: https://msdn.microsoft.com/en-us/library/aa720458%28v=vs.71%29.aspx 参见此处: https : //msdn.microsoft.com/zh-cn/library/aa720458%28v=vs.71%29.aspx
Try this: 尝试这个:
First split your text as follows: 首先按照以下步骤拆分文本:
string[] lines = xmldocument.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
Then you need to extract the content. 然后,您需要提取内容。
List<string> values = new List<string>(); //Here we save the Values according to your line numbers
NOTE: The list will start at Index 0 while your lines start at 1 => +1 when extracting!!! 注意:提取时,列表将从索引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.