loading an xml document from file vs. memory stream

Discussion in 'ASP .Net Web Services' started by John Hopper, May 19, 2005.

  1. John Hopper

    John Hopper Guest

    Hello,

    I need to read am xml node using an xmlNodeReader and selectSingleNode. This
    works fine when I save the file first and then use xmlDocument.load(uri),
    with the uri the location of the saved xml file. But when I try to load the
    xml document by passing the xmlDocument.load method a memory stream I get
    "root element is missing". When I convert the stream to a string using

    Dim theEncoding As New System.Text.UTF8Encoding
    Dim str As String = theEncoding.GetString(ms.GetBuffer)

    the xml looks like this: (Removed text was not removed in the snippet)

    <?xml version="1.0"?>
    <Administrative xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.removed/XMLSchemas/removed/schema.xsd">
    <CourtCode>1111</CourtCode>
    <ReportYear>2005</ReportYear>
    <ReportQuarter>3</ReportQuarter>
    <PreparerName>asdf</PreparerName>
    <PreparerPhone>3333</PreparerPhone>
    <Clerk>false</Clerk>
    </Administrative>

    and this is the same as in the saved xml file that loads fine:

    "<Administrative>
    <CourtCode>8888</CourtCode>
    <Password>password</Password>
    <ReportYear>2005</ReportYear>
    <ReportQuarter>4</ReportQuarter>
    <PreparerName>asdf</PreparerName>
    <PreparerPhone>4444444444</PreparerPhone>
    <Clerk>false</Clerk>
    </Administrative>"

    except for the namespace attributes. So the presence of those, which results
    from loading the xml document from a memory stream rather than from a saved
    file, seems to cause the "root element missing" exception to be thrown.

    How can I prevent the namespace attributes from appearing and causing this
    problem? Or is the a better way to load the file for node reading without
    first saving it?

    Thank you,

    John Hopper
     
    John Hopper, May 19, 2005
    #1
    1. Advertising

  2. John Hopper

    John Hopper Guest

    When I try:

    Dim buf As Byte() = System.Text.UTF8Encoding.UTF8.GetBytes(oReport.OuterXml)
    Dim ms As New MemoryStream(buf)
    Dim myXmlDataDoc As XmlDataDocument = New XmlDataDocument
    myXmlDataDoc.Load(ms)

    I get

    Object reference not set to an instance of an object.



    "John Hopper" wrote:

    > Hello,
    >
    > I need to read am xml node using an xmlNodeReader and selectSingleNode. This
    > works fine when I save the file first and then use xmlDocument.load(uri),
    > with the uri the location of the saved xml file. But when I try to load the
    > xml document by passing the xmlDocument.load method a memory stream I get
    > "root element is missing". When I convert the stream to a string using
    >
    > Dim theEncoding As New System.Text.UTF8Encoding
    > Dim str As String = theEncoding.GetString(ms.GetBuffer)
    >
    > the xml looks like this: (Removed text was not removed in the snippet)
    >
    > <?xml version="1.0"?>
    > <Administrative xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    > xmlns="http://www.removed/XMLSchemas/removed/schema.xsd">
    > <CourtCode>1111</CourtCode>
    > <ReportYear>2005</ReportYear>
    > <ReportQuarter>3</ReportQuarter>
    > <PreparerName>asdf</PreparerName>
    > <PreparerPhone>3333</PreparerPhone>
    > <Clerk>false</Clerk>
    > </Administrative>
    >
    > and this is the same as in the saved xml file that loads fine:
    >
    > "<Administrative>
    > <CourtCode>8888</CourtCode>
    > <Password>password</Password>
    > <ReportYear>2005</ReportYear>
    > <ReportQuarter>4</ReportQuarter>
    > <PreparerName>asdf</PreparerName>
    > <PreparerPhone>4444444444</PreparerPhone>
    > <Clerk>false</Clerk>
    > </Administrative>"
    >
    > except for the namespace attributes. So the presence of those, which results
    > from loading the xml document from a memory stream rather than from a saved
    > file, seems to cause the "root element missing" exception to be thrown.
    >
    > How can I prevent the namespace attributes from appearing and causing this
    > problem? Or is the a better way to load the file for node reading without
    > first saving it?
    >
    > Thank you,
    >
    > John Hopper
     
    John Hopper, May 19, 2005
    #2
    1. Advertising

  3. John Hopper

    John H W Guest

    John, I don't have experience using XmlDataDocument, but am a long-time
    programmer who uses a form of XML loading menus, etc. What I see is that
    <Administrative> is missing from the first snippet and not the second. I
    would think that this would cause the "root missing" error.

    John H W

    "John Hopper" wrote:

    > When I try:
    >
    > Dim buf As Byte() = System.Text.UTF8Encoding.UTF8.GetBytes(oReport.OuterXml)
    > Dim ms As New MemoryStream(buf)
    > Dim myXmlDataDoc As XmlDataDocument = New XmlDataDocument
    > myXmlDataDoc.Load(ms)
    >
    > I get
    >
    > Object reference not set to an instance of an object.
    >
    >
    >
    > "John Hopper" wrote:
    >
    > > Hello,
    > >
    > > I need to read am xml node using an xmlNodeReader and selectSingleNode. This
    > > works fine when I save the file first and then use xmlDocument.load(uri),
    > > with the uri the location of the saved xml file. But when I try to load the
    > > xml document by passing the xmlDocument.load method a memory stream I get
    > > "root element is missing". When I convert the stream to a string using
    > >
    > > Dim theEncoding As New System.Text.UTF8Encoding
    > > Dim str As String = theEncoding.GetString(ms.GetBuffer)
    > >
    > > the xml looks like this: (Removed text was not removed in the snippet)
    > >
    > > <?xml version="1.0"?>
    > > <Administrative xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    > > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    > > xmlns="http://www.removed/XMLSchemas/removed/schema.xsd">
    > > <CourtCode>1111</CourtCode>
    > > <ReportYear>2005</ReportYear>
    > > <ReportQuarter>3</ReportQuarter>
    > > <PreparerName>asdf</PreparerName>
    > > <PreparerPhone>3333</PreparerPhone>
    > > <Clerk>false</Clerk>
    > > </Administrative>
    > >
    > > and this is the same as in the saved xml file that loads fine:
    > >
    > > "<Administrative>
    > > <CourtCode>8888</CourtCode>
    > > <Password>password</Password>
    > > <ReportYear>2005</ReportYear>
    > > <ReportQuarter>4</ReportQuarter>
    > > <PreparerName>asdf</PreparerName>
    > > <PreparerPhone>4444444444</PreparerPhone>
    > > <Clerk>false</Clerk>
    > > </Administrative>"
    > >
    > > except for the namespace attributes. So the presence of those, which results
    > > from loading the xml document from a memory stream rather than from a saved
    > > file, seems to cause the "root element missing" exception to be thrown.
    > >
    > > How can I prevent the namespace attributes from appearing and causing this
    > > problem? Or is the a better way to load the file for node reading without
    > > first saving it?
    > >
    > > Thank you,
    > >
    > > John Hopper
     
    John H W, May 25, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Prichard
    Replies:
    0
    Views:
    742
    Tony Prichard
    Dec 12, 2003
  2. Manish Hatwalne
    Replies:
    1
    Views:
    401
    Martin Honnen
    Jul 13, 2004
  3. Replies:
    9
    Views:
    644
    Alex Buell
    Apr 27, 2006
  4. Ion Freeman
    Replies:
    17
    Views:
    834
    Tom Anderson
    Jul 11, 2009
  5. ofir
    Replies:
    0
    Views:
    187
Loading...

Share This Page