[英]How to read the nodes of xml file in c#, where the xml file is the combination of 2 xml file data?
我已經將2個xml文件數據合並為一個xml文件,格式如下
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="c:\users\Report.xsl"?>
<Report>
<Messages>
<Message>
My Data
</Message>
</Messages>
</Report>
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="c:\users\Report.xsl"?>
<Report>
<Messages>
<Message>
My Data
</Message>
</Messages>
</Report>
我想從<Message> </Message>
節點獲取文本數據。
我已經編寫了以下常用的xml加載代碼以獲取詳細信息。
XmlDocument doc = new XmlDocument();
doc.Load(Path + "\\result.xml");
但是我收到以下錯誤。
“意外的XML聲明。XML聲明必須是文檔中的第一個節點,並且不允許在其之前出現空格字符。第10行,位置3。”
是否由於存在兩個<?xml
聲明而導致錯誤? 如果是這樣,在<Message> </Message>
標記中獲取所有數據的最佳方法是什么?
此代碼
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="c:\users\Report.xsl"?>
一個XML文件在開始時只能包含一次。
請從結果文件的中間刪除這些行。
另外,請將您的XML包裝到一些根標簽中。
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="c:\users\Report.xsl"?>
<Root>
<Report>
<Messages>
<Message>
My Data
</Message>
</Messages>
</Report>
<Report>
<Messages>
<Message>
My Data
</Message>
</Messages>
</Report>
</Root>
基於@DotNet范答案。 刪除重復的<?xml
行,並用root元素包裝您的元素 。 這是代碼:
// read all the lines
var allLines = File.ReadAllLines(@"G:\TestFiles\TextFile1.txt");
var filtered =
allLines.Take(2). // take the first two lines i.e. the declaration
Concat(new string[] { "<Root>" }). // add a Root element start header
Concat(allLines.Where(l => !l.StartsWith("<?xml"))). // get all lines that do not start with <?xml
Concat(new string[] { "</Root>" }); // add the end header
string oneXmlFile = string.Join(Environment.NewLine, filtered); // join all lines into one string
XDocument document = XDocument.Parse(oneXmlFile); // read the new string as XML
這是XML結果文件
<?xml-stylesheet type="text/xsl" href="c:\users\Report.xsl"?>
<Root>
<Report>
<Messages>
<Message>
My Data
</Message>
</Messages>
</Report>
<Report>
<Messages>
<Message>
My Data
</Message>
</Messages>
</Report>
</Root>
以下代碼將正確讀取您的xml。 解決重復項
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
StreamReader reader = new StreamReader(FILENAME);
string input = "";
string xml = "";
while((input = reader.ReadLine()) != null)
{
if (!input.StartsWith("<?xml"))
{
xml += input;
}
}
StringReader sReader = new StringReader(xml);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
XmlReader xReader = XmlReader.Create(sReader, settings);
List<XElement> reports = new List<XElement>();
while (!xReader.EOF)
{
if (xReader.Name != "Report")
{
xReader.ReadToFollowing("Report");
}
if (!xReader.EOF)
{
reports.Add((XElement)XElement.ReadFrom(xReader));
}
}
}
}
}
刪除此代碼
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="c:\users\Report.xsl"?>
xml文件格式錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.