Reading XML from Stream

后端 未结 2 1479
旧巷少年郎
旧巷少年郎 2021-01-17 23:53

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

Stream inputStream = XmlFileUploa         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-18 00:28

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

    The CorrectFileFormat() method:

    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:

    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() + "

    "); } }

提交回复
热议问题