简体   繁体   English

如何在c#中读取xml文件的节点,其中xml文件是2个xml文件数据的组合?

[英]How to read the nodes of xml file in c#, where the xml file is the combination of 2 xml file data?

I have combined 2 xml file data into a single xml file, which will be in following syntax 我已经将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>

and I want to get the text data from <Message> </Message> node. 我想从<Message> </Message>节点获取文本数据。

I have written following usual xml load code to get the details. 我已经编写了以下常用的xml加载代码以获取详细信息。

            XmlDocument doc = new XmlDocument();
            doc.Load(Path + "\\result.xml"); 

But I am getting the following error. 但是我收到以下错误。

"Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 10, position 3." “意外的XML声明。XML声明必须是文档中的第一个节点,并且不允许在其之前出现空格字符。第10行,位置3。”

Is the error because of there are two <?xml declaration? 是否由于存在两个<?xml声明而导致错误? If so, what is the best way to get all the data within <Message> </Message> tag? 如果是这样,在<Message> </Message>标记中获取所有数据的最佳方法是什么?

This code 此代码

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="c:\users\Report.xsl"?>

can contain just once for a XML file, at its start. 一个XML文件在开始时只能包含一次。

Please remove these lines from the middle of your result file. 请从结果文件的中间删除这些行。

And also please wrap your XML into some root tag. 另外,请将您的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>

Based on @DotNet Fan answer. 基于@DotNet范答案。 Remove the duplicate <?xml lines and wrap your elements with a root element . 删除重复的<?xml行,并用root元素包装您的元素 Here is the code: 这是代码:

// 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

This is the XML result file 这是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>

Following code will reader your xml without errors. 以下代码将正确读取您的xml。 Worked around the duplicates 解决重复项

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

        }
    }
}

remove this code 删除此代码

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="c:\users\Report.xsl"?>

xml file has something format error xml文件格式错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM