简体   繁体   中英

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

<?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.

I have written following usual xml load code to get the details.

            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."

Is the error because of there are two <?xml declaration? If so, what is the best way to get all the data within <Message> </Message> tag?

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.

Please remove these lines from the middle of your result file.

And also please wrap your XML into some root tag.

<?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. Remove the duplicate <?xml lines and wrap your elements with a root element . 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-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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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