Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8392663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:34:15+00:00 2026-06-09T19:34:15+00:00

I getting xml through web service in string format and to pass through xmlreader,

  • 0

I am not able to find exact exception, when debugging, I can just find this infoI getting xml through web service in string format and to pass through xmlreader, I am converting the string to a stream object. But cannot figure out what I am missing. The xml and schema mentioned here is a sample.

class Program
{
    static void Main(string[] args)
    {
        try
        {

            XmlDocument doc = new XmlDocument();

            doc.Load("books.xml");
            StringWriter sw = new StringWriter();
            XmlTextWriter tx = new XmlTextWriter(sw);
            doc.WriteTo(tx);
            string leadxml = sw.ToString();

            XmlReaderSettings xmlSettings = new XmlReaderSettings();
            xmlSettings.Schemas = new System.Xml.Schema.XmlSchemaSet();
            xmlSettings.Schemas.Add(string.Empty,"books.xsd");
            xmlSettings.ValidationType = ValidationType.Schema;
            byte[] byteArray = Encoding.ASCII.GetBytes(leadxml);
            MemoryStream stream = new MemoryStream(byteArray);
            XmlReader reader = XmlReader.Create(stream, xmlSettings);

            // Parse the file.
            while (reader.Read());


        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);

        }
    }

}

xml is:

 <?xml version="1.0" encoding="utf-8" ?>
<bookstore>
  <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0"/>
</bookstore>

xsd is:

    <?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="bookstore">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="book">

        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:34:16+00:00Added an answer on June 9, 2026 at 7:34 pm

    First of all, if all your doing is getting the Xml into a string, this entire block of code is unnecessary:

              XmlDocument doc = new XmlDocument();
              doc.Load("books.xml");
              StringWriter sw = new StringWriter();
              XmlTextWriter tx = new XmlTextWriter(sw);
              doc.WriteTo(tx);
              string leadxml = sw.ToString();
    

    Instead just do:

              string leadxml = File.ReadAllText("books.xml");
    

    Secondly, if you have a string already, it is completely unnecessary to do all this conversion on the string. The XmlReader.Create method has a signature which accepts a TextReader. StringReader derives from TextReader. Therefore, this:

                 byte[] byteArray = Encoding.ASCII.GetBytes(leadxml);
                 MemoryStream stream = new MemoryStream(byteArray);
                 XmlReader reader = XmlReader.Create(stream, xmlSettings);
    

    Can be replaced with this:

                 StringReader leadxmlStringReader = new StringReader(leadXml);
                 XmlReader reader = XmlReader.Create(leadxmlStringReader, xmlSettings);
    

    For any further assistance with this question, you will need to provide Exception details and/or specific error messages.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting HTML response through web service and I want xml from web
I am invoking a web service through $.ajax(). onsuccess I am getting an xml
I am trying to send the XML data to a web service. But i
I am trying to invoke a web-service through stub. It is wsdl web-service But
I am getting some xml in a string variable through a wcf webservice. I
I am getting xml by webservice and want to display it to datagrid. But
I have an getting an xml response on this format <?xml version=\1.0\ encoding=\utf-8\?>\r\n <PlatformResponse
I'm getting this XML from a service that I need to deal with. I'm
i have used libxml2.Here when i am getting an XML data as a string
I'm running ant and added the below to build.xml but I'm getting:\ build.xml:51: Element

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.