Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# xmlns='';读取货币数据时不应出现此错误_C#_Xml_Webclient - Fatal编程技术网

C# xmlns='';读取货币数据时不应出现此错误

C# xmlns='';读取货币数据时不应出现此错误,c#,xml,webclient,C#,Xml,Webclient,我正在使用此代码从XE.COM读取XML数据 string url = ConfigurationManager.AppSettings[CONFIGURATION_KEY_XE_COM_URL]; System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(xedatafeed)); // try XmlReader XmlReaderSettings set

我正在使用此代码从XE.COM读取XML数据

string url = ConfigurationManager.AppSettings[CONFIGURATION_KEY_XE_COM_URL];

System.Xml.Serialization.XmlSerializer ser =
new System.Xml.Serialization.XmlSerializer(typeof(xedatafeed));


// try XmlReader
XmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = null;
settings.DtdProcessing = DtdProcessing.Parse;
System.Xml.XmlReader reader = System.Xml.XmlReader.Create(url, settings);
string reply = (string)ser.Deserialize(reader);

// try WebClient
System.Net.WebClient client = new System.Net.WebClient();
string data = Encoding.UTF8.GetString(client.DownloadData(url));
问题是这条线

xedatafeed reply = (string)ser.Deserialize(xedatafeed);
正在引发异常

<xe-datafeed xmlns=''> was not expected.
不是预期的。

如何修复此问题?

数据似乎返回了以下形式的xml:

<xe-datafeed>...</xe-datafeed>
然后在代码中使用:

var ser = new XmlSerializer(typeof(XeDataFeed));
//...
var obj = (XeDataFeed)ser.Deserialize(reader);
如果模型较大,也可以使用
xsd.exe
工具在此处提供帮助:

xsd some.xml
(它检查
some.xml
并生成
some.xsd


(它检查
some.xsd
,并生成
some.cs
,其中包含要匹配的适当类)

只是为这个问题添加了一个非常具体的解决方案,看看@MarcGravel对通用解决方案的回答

> wget http://www.xe.com/datafeed/samples/sample-xml-usd.xml
> xsd sample-xml-usd.xml
// generating xsd, change xs:string to xs:decimal for crate and cinverse though
> xsd sample-xml-usd.xsd /classes
// now we have created sample-xml-usd.cs, c# classes representing the xml
最后的C#代码:

xedatafeed reply=null;
使用(var wc=new WebClient())//捕捉异常是为pussie服务的!:)
reply=ParseXml(wc.DownloadString(uri));
定义了以下方法:

T ParseXml<T>(string data) {
  return (T) new XmlSerializer(typeof(T)).Deserialize(new StringReader(data));
}
T解析XML(字符串数据){
返回(T)新的XmlSerializer(typeof(T))。反序列化(新的StringReader(数据));
}

我的猜测是,您在
LinqToXSD
及其生成方面遇到了一些问题。

如果数据不是用XmlSerializer序列化的,那么不清楚为什么您希望能够像那样反序列化它,或者为什么您希望结果是单个字符串。
XmlSerializer
对于
typeof(string)会是什么
甚至看起来像?@JonSkeet我实际上已经使用LinqToXSD为这个xml生成了一个对象,并在最初将该对象放在字符串的位置,但这引发了相同的错误。基本上,我想做的是调用一个url来获取一些xml,然后将其放回一个对象中,并且得到了这个错误。@SachinKainth如果你想将其放回一个对象中,为什么要使用
typeof(string)
(string)ser.Deserialize(reader)
?我已经将我的代码更新到了我原来的版本。我已经这样做了,但是使用了LinqToXSD而不是XSD。我该怎么办?我不想修改自动生成的代码,因为下一次生成代码时可能会被覆盖。请查看上面我正在使用的对象的代码更新,而不是字符串。噢,您可以使用
xsd.exe从
xml
生成
xsd:s
?但是在阅读
xsd.exe
帮助时,它似乎无法从多个
xml文件中执行某种类型的LCD
xsd
?我不知道,午饭后吃点东西总是有用的:)这段代码有效——唯一的问题是它需要我向自动生成的代码中添加一些可能被覆盖的xml。我想从url中获取一个字符串,然后将名称空间附加到该字符串,然后反序列化。
> wget http://www.xe.com/datafeed/samples/sample-xml-usd.xml
> xsd sample-xml-usd.xml
// generating xsd, change xs:string to xs:decimal for crate and cinverse though
> xsd sample-xml-usd.xsd /classes
// now we have created sample-xml-usd.cs, c# classes representing the xml
xedatafeed reply = null;

using (var wc = new WebClient()) // Catching exceptions is for pussies! :)
  reply = ParseXml<xedatafeed>(wc.DownloadString(uri));
T ParseXml<T>(string data) {
  return (T) new XmlSerializer(typeof(T)).Deserialize(new StringReader(data));
}