C# XML字符串中的无效字符

C# XML字符串中的无效字符,c#,asp.net,xml,C#,Asp.net,Xml,我从第三方收到一个xml字符串。xml字符串包含无效字符,如&和“”。我正在尝试将其放入数据集(ASP.NET)。它抛出错误。请任何人帮忙。使用xml导出数据的最佳方法是 <![CDATA[Your data goes here.]]> 但是,当您使用第三方xml时,请尝试使用它来处理xml中的特殊通道。使用xml导出数据的最佳方法是 <![CDATA[Your data goes here.]]> 但是,当您使用第三方xml时,请尝试使用它来处理xml中的特

我从第三方收到一个xml字符串。xml字符串包含无效字符,如&和“”。我正在尝试将其放入数据集(ASP.NET)。它抛出错误。请任何人帮忙。

使用xml导出数据的最佳方法是

<![CDATA[Your data goes here.]]>


但是,当您使用第三方xml时,请尝试使用它来处理xml中的特殊通道。

使用xml导出数据的最佳方法是

<![CDATA[Your data goes here.]]>


但是,当您使用第三方xml时,请尝试使用它来处理xml中的特殊更改。

告诉/要求第三方提供有效的xml

互操作性标准在没有得到遵守的情况下并不重要。如果他们今天向您传递无效字符,那么如何阻止他们明天传递不匹配的节点?或者根本没有标签

如果没有标准,那么就有无数的场景需要编写代码

也就是说,你可以:

  • 确保您的代码中没有问题(如果需要详细信息,请发布代码)
  • 创建已知查找/替换方案的可配置列表,并预处理输入的“XML”字符串
  • 如果数据完整性不重要(我个人认为一直如此),您可以将数据加载到HTML解析器中,这将更加宽容,并允许类似XML DOM的文档访问
根据OP的评论,这里有一个非常非常简单的示例,可以配置查找/替换

public string PreProcessXml( string xml )
{
    // this list could be read from a config file

    List<Tuple<string, string>> replacements = new List<Tuple<string, string>>();

    // Important: if there are VALID uses of an ampersand in your document, 
    // this may invalidate them! Perform a more elaborate check using a 
    // regex, or ensure that there are no valid entities already in the document.
    replacements.Add( new Tuple<string, string>( "&", "&amp;" ) );

    replacements.Add( new Tuple<string, string>( "\"", "&quot;" ) );
    replacements.Add( new Tuple<string, string>( "\'", "&apos;" ) );

    foreach( var replacement in replacements )
    {
        xml = xml .Replace( replacement.Item1, replacement.Item2 );
    }

     return xml;
}
公共字符串预处理xml(字符串xml)
{
//此列表可以从配置文件中读取
列表替换=新列表();
//重要提示:如果您的文档中存在有效的“与”符号使用,
//这可能会使它们失效!请使用
//或者确保文档中没有有效的实体。
添加(新元组(“&”、“&;”);
添加(新元组(“\”,“”);
添加(新元组(“\”,“&apos;”);
foreach(替换中的var替换)
{
xml=xml.Replace(replacement.Item1,replacement.Item2);
}
返回xml;
}

告诉/要求第三方提供有效的XML

互操作性标准在没有得到遵守的情况下并不重要。如果他们今天向您传递无效字符,那么如何阻止他们明天传递不匹配的节点?或者根本没有标签

如果没有标准,那么就有无数的场景需要编写代码

也就是说,你可以:

  • 确保您的代码中没有问题(如果需要详细信息,请发布代码)
  • 创建已知查找/替换方案的可配置列表,并预处理输入的“XML”字符串
  • 如果数据完整性不重要(我个人认为一直如此),您可以将数据加载到HTML解析器中,这将更加宽容,并允许类似XML DOM的文档访问
根据OP的评论,这里有一个非常非常简单的示例,可以配置查找/替换

public string PreProcessXml( string xml )
{
    // this list could be read from a config file

    List<Tuple<string, string>> replacements = new List<Tuple<string, string>>();

    // Important: if there are VALID uses of an ampersand in your document, 
    // this may invalidate them! Perform a more elaborate check using a 
    // regex, or ensure that there are no valid entities already in the document.
    replacements.Add( new Tuple<string, string>( "&", "&amp;" ) );

    replacements.Add( new Tuple<string, string>( "\"", "&quot;" ) );
    replacements.Add( new Tuple<string, string>( "\'", "&apos;" ) );

    foreach( var replacement in replacements )
    {
        xml = xml .Replace( replacement.Item1, replacement.Item2 );
    }

     return xml;
}
公共字符串预处理xml(字符串xml)
{
//此列表可以从配置文件中读取
列表替换=新列表();
//重要提示:如果您的文档中存在有效的“与”符号使用,
//这可能会使它们失效!请使用
//或者确保文档中没有有效的实体。
添加(新元组(“&”、“&;”);
添加(新元组(“\”,“”);
添加(新元组(“\”,“&apos;”);
foreach(替换中的var替换)
{
xml=xml.Replace(replacement.Item1,replacement.Item2);
}
返回xml;
}

xml字符串中的无效字符。公司名称包含无效字符&BLAUS Blauer请参阅Delikatessen Hanna Moos testInvalid字符的xml字符串。公司名称包含无效字符&BLAUS Blauer请参阅Delikatessen Hanna Moos TestTestSystem.Security.SecurityElement.Escape(xmlString);它还删除了xml节点的“”。。有没有解决办法。。谢谢..@srikanth-System.SecurityElement.Escape被记录为执行5个字符的替换:。手动替换所需的3个字符串,并省略转义标记字符的两个字符串。+1最重要的是:要求第三方提供格式良好的XML-其他一切都不可接受。trusted System.Security.SecurityElement.escape(xmlString);它还删除了xml节点的“”。。有没有解决办法。。谢谢..@srikanth-System.SecurityElement.Escape被记录为执行5个字符的替换:。手动替换所需的3个字符串,并省略转义标记字符的两个字符串。+1最重要的是:要求第三方提供格式良好的XML—其他一切都是不可接受的。