C# 从流中读取 XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17496175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:39:04  来源:igfitidea点击:

Reading XML from Stream

c#xmlstreamxmlreader

提问by Adam_G

I'm working with ASP.NET, and am importing an XML file from a form. Right now I convert that into a Stream:

我正在使用 ASP.NET,并且正在从表单导入 XML 文件。现在我将其转换为Stream

Stream inputStream = XmlFileUploadControl.PostedFile.InputStream;

because I may need this version later.

因为我以后可能需要这个版本。

I'd like to first check to make make sure that the XML file has the correct format, and, if it is, then display some information:

我想首先检查以确保 XML 文件具有正确的格式,如果是,则显示一些信息:

if (CorrectFileFormat(inputStream))
{
    DisplayLicenseInfo(inputStream);
}

else
{
    StatusLabel.Text = "Selected file is not a LicensingDiag XML file";
}

The CorrectFileFormat()method:

CorrectFileFormat()方法:

protected Boolean CorrectFileFormat(Stream inputStream)
{

    XmlReader reader = XmlReader.Create(inputStream);

    if (reader.MoveToContent() == XmlNodeType.Element && reader.Name == "DiagReport")
    {
        return true;
    }
}

The DisplayLicenseInfo()method:

DisplayLicenseInfo()方法:

protected void DisplayLicenseInfo(Stream inputStream)
{

    XmlReader reader = XmlReader.Create(inputStream);

    if (reader.MoveToContent() == XmlNodeType.Element && reader.Name == "LicensingStatus")
    {
        StatusLabel.Text += ("Licensing Status: " + reader.ReadString() + "<br><br>");
    }

}

However, I'm encountering an XmlExceptionthat says "Data at the root level is invalid. Line 1, position 1". Is this because I've already read through the input stream once, and need to reset it? If so, how do I do that?

但是,我遇到了XmlException“根级别的数据无效。第 1 行,位置 1”。这是因为我已经读过一次输入流,需要重置它吗?如果是这样,我该怎么做?

采纳答案by Thomas Levesque

The first time you create an XmlReaderaround the stream, it is at position 0. But the second time you create an XmlReader, the stream has already been partially read, so it is no longer at position 0, so the XmlReadercan't read the XML document.

第一次XmlReader在流周围创建一个,它在位置 0。但是第二次创建一个XmlReader,流已经被部分读取,所以它不再在位置 0,所以XmlReader无法读取 XML 文档.

Instead, you should create the XmlReaderonly once:

相反,您应该XmlReader只创建一次:

using (XmlReader reader = XmlReader.Create(inputStream)
{
    if (CorrectFileFormat(reader))
    {
        DisplayLicenseInfo(reader);
    }
    else
    {
        StatusLabel.Text = "Selected file is not a LicensingDiag XML file";
    }
}

If the file is small, you could also consider loading the entire XML document using XmlDocumentor XDocument(Linq to XML)

如果文件很小,您还可以考虑使用XmlDocumentor XDocument(Linq to XML)加载整个 XML 文档

回答by Lin Song Yang

@thomas-levesque https://stackoverflow.com/users/98713/thomas-levesquewas right, if the content itself is well-formed, then you need to rewind the stream back to the start of the content.

@thomas-levesque https://stackoverflow.com/users/98713/thomas-levesque是对的,如果内容本身格式正确,那么您需要将流倒回到内容的开头。

The CorrectFileFormat() method:

CorrectFileFormat() 方法:

protected Boolean CorrectFileFormat(Stream inputStream)
{
    // rewind the stream back to the very beginning of the content
    inputStream.Seek(0L, SeekOrigin.Begin);
    XmlReader reader = XmlReader.Create(inputStream);

    if (reader.MoveToContent() == XmlNodeType.Element && reader.Name == "DiagReport")
    {
        return true;
    }
}

The DisplayLicenseInfo() method:

DisplayLicenseInfo() 方法:

protected void DisplayLicenseInfo(Stream inputStream)
{
    // rewind the stream back to the very beginning of the content
    inputStream.Seek(0L, SeekOrigin.Begin);
    XmlReader reader = XmlReader.Create(inputStream);

    if (reader.MoveToContent() == XmlNodeType.Element && reader.Name == "LicensingStatus")
    {
        StatusLabel.Text += ("Licensing Status: " + reader.ReadString() + "<br><br>");
    }
}