路径- c#中的非法字符,读取xml

时间:2021-07-01 09:09:13

Im trying to read xml into SQL and get an error at the "ds.ReadXml("C:\test.xml");" line, any hints as to whats wrong? Thanks

我正在尝试将xml读入SQL并在“ds.ReadXml”(“C:\test.xml”);谢谢

tring connectionString = "Data Source=MyServer\\SQLEXPRESS;Initial Catalog=MyCatalog;Password=xxx;Persist Security Info=True;User ID=xxx";
        DataSet ds = new DataSet();
        DataTable sourcedata = new DataTable();
        ds.ReadXml("C:\test.xml");

        sourcedata = ds.Tables[0];

        using (SqlConnection sqlconn = new SqlConnection(connectionString))
        {
            sqlconn.Open();
            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlconn))
            {
                bulkCopy.ColumnMappings.Add("ID", "ID");
                bulkCopy.ColumnMappings.Add("user_id", "user_id");
                bulkCopy.ColumnMappings.Add("upload_date", "upload_date");
                bulkCopy.ColumnMappings.Add("userAddr", "userAddr");
                bulkCopy.ColumnMappings.Add("serial_no", "serial_no");
                bulkCopy.ColumnMappings.Add("Mfgr", "Mfgr");
                bulkCopy.ColumnMappings.Add("model", "model");
                bulkCopy.ColumnMappings.Add("description", "description");
                bulkCopy.ColumnMappings.Add("type", "type");
                bulkCopy.ColumnMappings.Add("code", "code");
                bulkCopy.ColumnMappings.Add("comments", "comments");
                bulkCopy.ColumnMappings.Add("qty", "qty");
                bulkCopy.ColumnMappings.Add("condition", "condition");
                bulkCopy.ColumnMappings.Add("location", "location");
                bulkCopy.ColumnMappings.Add("price", "price");
                bulkCopy.DestinationTableName = "Upload";
                bulkCopy.WriteToServer(sourcedata);
            }
        }
    }

8 个解决方案

#1


5  

Your C:\test.xml is being read as C:(tab character)est.xml. Put a @ in front of your string: @"C:\test.xml", or escape it with "C:\\test.xml".

你的C:\测试。xml被读为C:(制表符)est.xml。在你的字符串前面放一个@:@“C:\test”。xml,或者用“C:\ test.xml”转义。

9 times out of ten, you'd get an error message along the lines of 'illegal escape character' when you make this mistake and it would be a lot more obvious what's wrong, but in this case it just happens to be legal.

十有八九,当你犯了这个错误时,你会得到一条“非法转义字符”的错误信息,而且错误的地方会明显得多,但在这种情况下,这是合法的。

#2


4  

Try escaping '\' symbol:

试着逃避“\”符号:

ds.ReadXml("C:\\test.xml");

#3


3  

try:

试一试:

ds.ReadXml(@"C:\test.xml");

\t is seen as a horizontal tab character, unless you escape it.

\t被视为水平制表符,除非您转义它。

#4


2  

Try Changing this:

试着改变:

ds.ReadXml("C:\test.xml"); 

with

ds.ReadXml("C:\\test.xml"); 

#5


2  

You need to escape \, e.g.: ds.ReadXml("C:\\test.xml"); or ds.ReadXml(@"C:\test.xml");

您需要退出\,例如:ds.ReadXml(“C:\\test.xml”);或ds.ReadXml(@“C:\ test.xml”);

#6


1  

your problem is that your \ character will need escaping. At the moment it is being read as \t which is a tab character and invalid. You want either:

你的问题是你的性格需要摆脱。目前它被读取为\t,这是一个制表符,无效。你想要的:

ds.ReadXml(@"C:\test.xml");
ds.ReadXml("C:\\test.xml");

#7


0  

You could put a @ before your string:

你可以在字符串前加上@:

ds.ReadXml(@"C:\test.xml"); 

This is called a verbatim string literal

这被称为逐字字符串字面量

#8


0  

Hope your xml file format is correct .You just use ds.ReadXml(@"C:\test.xml"); Insteaed of ds.ReadXml("C:\test.xml");

希望您的xml文件格式是正确的。您只需使用ds.ReadXml(@“C:\test.xml”);输送链条的ds.ReadXml(“C:\ test.xml”);

#1


5  

Your C:\test.xml is being read as C:(tab character)est.xml. Put a @ in front of your string: @"C:\test.xml", or escape it with "C:\\test.xml".

你的C:\测试。xml被读为C:(制表符)est.xml。在你的字符串前面放一个@:@“C:\test”。xml,或者用“C:\ test.xml”转义。

9 times out of ten, you'd get an error message along the lines of 'illegal escape character' when you make this mistake and it would be a lot more obvious what's wrong, but in this case it just happens to be legal.

十有八九,当你犯了这个错误时,你会得到一条“非法转义字符”的错误信息,而且错误的地方会明显得多,但在这种情况下,这是合法的。

#2


4  

Try escaping '\' symbol:

试着逃避“\”符号:

ds.ReadXml("C:\\test.xml");

#3


3  

try:

试一试:

ds.ReadXml(@"C:\test.xml");

\t is seen as a horizontal tab character, unless you escape it.

\t被视为水平制表符,除非您转义它。

#4


2  

Try Changing this:

试着改变:

ds.ReadXml("C:\test.xml"); 

with

ds.ReadXml("C:\\test.xml"); 

#5


2  

You need to escape \, e.g.: ds.ReadXml("C:\\test.xml"); or ds.ReadXml(@"C:\test.xml");

您需要退出\,例如:ds.ReadXml(“C:\\test.xml”);或ds.ReadXml(@“C:\ test.xml”);

#6


1  

your problem is that your \ character will need escaping. At the moment it is being read as \t which is a tab character and invalid. You want either:

你的问题是你的性格需要摆脱。目前它被读取为\t,这是一个制表符,无效。你想要的:

ds.ReadXml(@"C:\test.xml");
ds.ReadXml("C:\\test.xml");

#7


0  

You could put a @ before your string:

你可以在字符串前加上@:

ds.ReadXml(@"C:\test.xml"); 

This is called a verbatim string literal

这被称为逐字字符串字面量

#8


0  

Hope your xml file format is correct .You just use ds.ReadXml(@"C:\test.xml"); Insteaed of ds.ReadXml("C:\test.xml");

希望您的xml文件格式是正确的。您只需使用ds.ReadXml(@“C:\test.xml”);输送链条的ds.ReadXml(“C:\ test.xml”);