Web service to Upload XML string to DB : gets error

I have a web service that accepts the xml string and uploads to the database..

  [WebMethod]
        public void Service(string XML)
        {
            try
            {
                using (DBDataContext db = new DBDataContext())
                {
                    db.ImportXML(XElement.Parse(XML));
                }

               
            }
            catch (Exception err)
            {
               
                throw err;
            }
        }

iam getting the below error
HTTP Transport error javax.xml.soap.SOAPException Error parsing envelope: (77,40) Invalid char in Text.

i know it is occurning beceause of some invalid characters in the xml string..
is there any other way to do it.. can anyone help.

also what do i need to do if i need to upload 100mb data..
gautam_reddycAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Richard QuadlingConnect With a Mentor Senior Software DeveloperCommented:
Not being rude, but good luck.

What criteria makes "bad data"?

That's why we have a parser to do this for us. It is inordinately complicated.

In essence you need to parse every character to determine if it < is for / or just some orphaned < in the middle of nowhere.

#1089 really shouldn't be an issue.

<?xml version="1.0"?>
#1089

is perfectly valid.

But ...

Assuming you CAN get hold of the XML before it gets to the parser ...

Using the regex ...

>(?![\r\n]++)([^<]*?)(?![\r\n]++)<(?![\r\n]++)([^<]*?)(?![\r\n]++)<

could work.


Dim ResultString As String
Try
	ResultString = Regex.Replace(XMLString, ">(?![\r\n]+)([^<]*?)(?![\r\n]+)<(?![\r\n]+)([^<]*?)(?![\r\n]+)<", ">$1&lt;$2<", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
Catch ex As ArgumentException
	'Syntax error in the regular expression
End Try

Open in new window

0
 
alexey_gusevCommented:
first of all, you should detect what character is invalid.

in general, you might want to encode your xml string before sending it to ws
0
 
Richard QuadlingSenior Software DeveloperCommented:
Line 77, character 40 would be were I would start.

At a guess, you have a < or a >.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
gautam_reddycAuthor Commented:
the special characters are # and &..
0
 
gautam_reddycAuthor Commented:
we are on the web service side.. we have no control of what is being sent as input.. how do we parse it
0
 
alexey_gusevCommented:
replace xml special chars (like < by &amp;gt;)
0
 
Richard QuadlingSenior Software DeveloperCommented:
If it is a real soap service, then you reject it. It isn't valid XML and that's a requirement.

Can you show the node with the bad chars?

# doesn't ring any bells as far as I know.

& => &amp;

Unless you have something like ...

&#123; and you want that as the 6 characters, rather than { it represents.

In that case ...

&amp;#123; should be used.

Again, it isn't your concern normally as this is bad data.

0
 
gautam_reddycAuthor Commented:
it is in the comments tag
 <COMMENT>Item #1089 building </COMMENT>.. there are bunch of comments tag..
how to replace all.. any idea?
0
 
gautam_reddycAuthor Commented:
i know this is a bad data.. but we need to parse the entire xml string. identify all the special characters and replace them with good data..
0
 
gautam_reddycAuthor Commented:
Works like a charm.. thanks..buddy
0
 
Richard QuadlingSenior Software DeveloperCommented:
Woo Hoo!
0
All Courses

From novice to tech pro — start learning today.