简体   繁体   English

在C#中加载XML文件

[英]XML file loading in c#

I have a problem with loading an XML file by: 我在通过以下方式加载XML文件时遇到问题:

XDocument.Load("file.xml");

Here are some of the errors: 以下是一些错误:

error CS1056: Unexpected character '®'
error CS1056: Unexpected character '™'
error CS1056: Unexpected character '、'
error CS0116: A namespace cannot directly contain members such as fields or methods
error CS1003: Syntax error, ']' expected
error CS1518: Expected class, delegate, enum, interface, or struct

So I wanted to ask if there is any way to fix this easily? 因此,我想问一下是否有任何方法可以轻松解决此问题?

You need to escape those characters in the xml file. 您需要在xml文件中转义那些字符。 The best way would be to do this uppon generation of the XML File. 最好的方法是完成XML文件的生成。 But if that is no option for you you coul read in the file line by line, replace the charcters by the corresponding escap sequence and write the lines back to another file. 但是,如果那不是您的选择,则可以逐行读取文件,将字符替换为相应的escap序列,并将这些行写回到另一个文件中。 This file would be the source for your XDocument. 该文件将成为您的XDocument的源。

尝试将其作为XML文件的第一行。

<?xml version="1.0" encoding="UTF-8"?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM