C# XML url中的符号未传递
我很难解决url中的符号(&)这个小问题。。。我正在序列化XML,如下所示C# XML url中的符号未传递,c#,.net,xml,xml-serialization,xmlserializer,C#,.net,Xml,Xml Serialization,Xmlserializer,我很难解决url中的符号(&)这个小问题。。。我正在序列化XML,如下所示 var ser = new XmlSerializer(typeof(response)); using (var reader = XmlReader.Create(url)) { response employeeResults = (response)ser.Deserialize(reader); //<<error when i pass with amper
var ser = new XmlSerializer(typeof(response));
using (var reader = XmlReader.Create(url))
{
response employeeResults = (response)ser.Deserialize(reader); //<<error when i pass with ampersand
}
我对此url有问题:
http://api.host.com/api/employees.xml/?&search=john
http://api.host.com/api/employees.xml/?&max=20&page=10
我得到的错误是:
`There is an error in XML document (1, 389).`
PS:我试过通过
&
并使用和#38
或026
或&代码>-运气不好。您是否尝试用
包装属性?
&不允许在xml中使用
此XML格式不正确:
<?xml version="1.0"?>
<response xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Api">
<meta>
<status>200</status>
<message />
<resultSet>
<Checked>true</Checked>
</resultSet>
<pagination>
<count>1</count>
<page>1</page>
<max>1</max>
<curUri>http://api.host.com/employee.xml/?&max=5</curUri>
<prevUri i:nil="true"/>
<nextUri>http://api.host.com/employee.xml/?&max=5&page=2</nextUri>
</pagination>
</meta>
<results i:type="ArrayOfemployeeItem">
<empItem>
<Id>CTR3242</Id>
<name>john</name>
......
</empItem>
</results>
</response>
如果您处理的是某个第三方系统,并且无法获得正确的XML响应,则必须进行一些预处理
也许最简单的方法就是用替换所有&
代码>使用字符串。替换方法
或者使用此正则表达式替换所有的&
,不包括正确的&
显示您试图反序列化的响应
类声明和整个XML。您是序列化还是反序列化?我已经更新了我的问题。请看一下在反序列化之前您将在哪里包装?也许您可以在反序列化之前预处理数据,以在反序列化之前删除&?替换&
?我该怎么做?我得到的是var reader=xmlreader.create(url)
@AbuHamzah,使用WebClient.DownloadString将您的响应作为字符串:所以在xml中使用与是一件坏事?有没有网站或博客告诉我什么是xml中不允许的,这样我就可以告诉大家不要提供xml不喜欢的字符了。-谢谢。+1(另外,对“无效XML”进行任何形式的字符串替换以使其格式正确通常是个坏主意,强制使用格式正确的XML要好得多)。是查找XML规范的最佳位置。参见
<?xml version="1.0"?>
<response xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Api">
<meta>
<status>200</status>
<message />
<resultSet>
<Checked>true</Checked>
</resultSet>
<pagination>
<count>1</count>
<page>1</page>
<max>1</max>
<curUri><![CDATA[http://api.host.com/employee.xml/?&max=5]]></curUri>
<prevUri i:nil="true"/>
<nextUri><![CDATA[http://api.host.com/employee.xml/?&max=5&page=2]]></nextUri>
</pagination>
</meta>
<results i:type="ArrayOfemployeeItem">
<empItem>
<Id>CTR3242</Id>
<name>john</name>
......
</empItem>
</results>
</response>