XElement.Load Error reading ampersand symbols and special country characters

后端 未结 1 607
闹比i
闹比i 2021-01-25 11:43

I\'m having problems reading the ampersand symbol from an XML file:

XElement xmlElements = XElement.Load(Path_Xml_Data_File);

I get error when

1条回答
  •  鱼传尺愫
    2021-01-25 12:28

    I'm afraid that you're dealing with malformed XML.

    To represent the ampersand, the data that you're loading should use the "&" entity.

    The ç (ç) and ã (ã) named entities are not part of the XML standard, they are more commonly found in HTML (although they can be added to XML by the use of a DTD).

    You could use HtmlTidy to tidy up the data first, or you could write something to convert the bare ampersands into entities on the incoming files.

    For example:

    public string CleanUpData(string data)
    {
        var r = new Regex(@"&\s");
        string output = r.Replace(data, "& ");
        output = output.Replace("ç", "ç");
        output = output.Replace("ã", "ã");
        return output;
    }
    

    0 讨论(0)
提交回复
热议问题