我正在寻找快速反序列化xml的方法,它有像ö那样的特殊字符.
我使用的是XMLReader,但无法对这些字符进行反序列化.
有什么建议吗?
编辑:我正在使用C#.代码如下:
XElement element =.. //has the xml
XmlSerializer serializer = new XmlSerializer(typeof(MyType));
XmlReader reader = element.CreateReader();
Object o= serializer.Deserialize(reader);
Run Code Online (Sandbox Code Playgroud) 我似乎无法弄清楚为什么这个测试没有通过
测试是:
给出以下XML:
<?xml version="1.0" encoding="utf-8"?>
<foo>
<account>
1234567890
</account>
<deptCode>
ABCXYZ
</deptCode>
</foo>
Run Code Online (Sandbox Code Playgroud)
和以下课程:
class Foo {
[XmlElement(ElementName = "account", DataType = "normalizedString")]
string account;
[XmlElement(ElementName = "deptCode", DataType = "normalizedString"]
string deptCode;
}
Run Code Online (Sandbox Code Playgroud)
当使用以下方式对XML进行反序列化时:
XmlSerializer serializer = new XmlSerializer(typeof(Foo));
Foo myFoo = (Foo) serializer.Deserialize(xmlReader);
Run Code Online (Sandbox Code Playgroud)
我得到以下值:
Foo.account = "\r\n 1234567890 \r\n"
Foo.deptCode = "\r\n ABCXYZ \r\n"
Run Code Online (Sandbox Code Playgroud)
而不是预期的
Foo.account = "1234567890"
Foo.deptCode = "ABCXYZ"
Run Code Online (Sandbox Code Playgroud)
我怎样才能使反序列化过程给我预期的结果?我认为DataType="normalizedString"可能会这样做,但它似乎没有效果,当我使用时XmlReaderSettings.IgnoreWhitespace,它只会带走"\ r"字符,留下"\n 1234567890"
考虑以下XML:
<a>
<b>2</b>
<c></c>
</a>
Run Code Online (Sandbox Code Playgroud)
我需要将这个xml反序列化为一个对象.所以,我写了下面的课.
public class A
{
[XmlElement("b", Namespace = "")]
public int? B { get; set; }
[XmlElement("c", Namespace = "")]
public int? C { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
因为我正在使用nullables,所以我期待在解析上面的xml时,我会得到一个带有null C属性的对象A.
而不是这个,我得到一个异常,告诉文档有错误.
我有这个代码:
[XmlType( "Metadata" )]
[Serializable]
public class MetadataContainer : List<MetadataBase>
{
}
[XmlType( "Meta" )]
[XmlInclude( typeof( ReadonlyMetadata ) )]
[Serializable]
public abstract class MetadataBase
{
}
[XmlType( "Readonly" )]
[Serializable]
public class ReadonlyMetadata : MetadataBase
{
}
[TestFixture]
public class SerializationTests
{
[Test]
public void Can_deserialize_with_known_type()
{
const string text = @"<Metadata xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
<Meta xsi:type=""Readonly"" />
</Metadata>";
var serializer = new XmlSerializer( typeof( MetadataContainer ) );
var metas = (MetadataContainer)serializer.Deserialize( XmlReader.Create( new StringReader( text ) ) );
Assert.That( …Run Code Online (Sandbox Code Playgroud) 我正在为Java ws制作.net wcf客户端。但是当我发出一个请求,并且响应包含错误时,它也包含multiRef部分。所以我得到这个异常:
服务器返回了无效的SOAP错误。来自名称空间“ http://schemas.xmlsoap.org/soap/envelope的结束元素” Body” 。从名称空间中找到元素'multiRef'''
这是服务器响应的样子(直接从网络获取):
<soapenv:Envelope>
<soapenv:Header/>
<soapenv:Body>
<soapenv:Fault>...</soapenv:Fault>
<multiRef>...</multiRef>
</soapenv:Body>
</soapenv:Envelope>
Run Code Online (Sandbox Code Playgroud)
有什么方法可以配置我的wcf客户端以反序列化该消息吗?还是有可能获取原始响应xml?
为简单起见,我将使用水果显示我的示例代码.实际上我正在做一些更有意义的事情(我们希望).假设我们有一个枚举:
public enum FruitType
{
Apple,
Orange,
Banana
}
Run Code Online (Sandbox Code Playgroud)
一节课:
[Serializable]
public class Fruit
{
public FruitType FruitType { get; set; }
public Fruit(FruitType type)
{
this.FruitType = type;
}
}
Run Code Online (Sandbox Code Playgroud)
我们可以对它进行序列化和反序列化.现在,让我们修改枚举,现在它是:
public enum FruitType
{
GreenApple,
RedApple,
Orange,
Banana
}
Run Code Online (Sandbox Code Playgroud)
反序列化以前序列化的对象时,会出现System.InvalidOperation异常,因为Apple(原始枚举项)无效.该对象不会被反序列化.
我能够解决这个问题的一种方法是在序列化时为类中的FruitType属性赋予Fruit不同的名称,如下所示:
[XmlElement(ElementName = "Mode")]
public FruitType FruitType { get; set; }
Run Code Online (Sandbox Code Playgroud)
现在,在反序列化期间,旧属性将被忽略,因为找不到它.我想知道是否有一种方法可以在反序列化期间忽略/跳过无效的枚举项,这样就不会抛出异常并且对象仍然会被反序列化.
c# serialization xml-serialization xml-deserialization deserialization
我有一个.Net Webservice,它返回一个简单的序列化对象.
我有以下代码来获取我的.Net webservice的响应.如何将返回的XML反序列化为我的对象?
我想返回MyObject而不是SoapPrimitive.
private static SoapPrimitive callWebServiceMethod(String url,
String namespace, String methodName,
HashMap<String, Object> parameters, String soapAction)
throws Exception {
Log.i("WebService", "URL: " + url);
Log.i("WebService", "MethodName: " + methodName);
URL myurl = new URL(url);
URLConnection connection = myurl.openConnection();
connection.setConnectTimeout(20 * 1000);
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK)
{
httpConnection.disconnect();
SoapObject request = new SoapObject(namespace, methodName);
if (parameters != null) {
String[] keys = new String[0];
keys = (String[]) parameters.keySet().toArray(keys);
Object[] …Run Code Online (Sandbox Code Playgroud) 我通过API收集xml格式的一些数据,并希望在对象列表中反序列化它.我正在使用Symfony2并找出JMSSerializerBundle,但我真的不知道如何使用它.
我知道Sf2允许将对象序列化/反序列化到数组,但我正在寻找更具体的东西.例如,对于这个类:
class Screenshot
{
/**
* @var integer $id
*/
private $id;
/**
* @var string $url_screenshot
*/
private $url_screenshot;
public function __construct($id, $url_screenshot) {
$this->id = $id;
$this->url_screenshot = $url_screenshot;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set url_screenshot
*
* @param string $urlScreenshot
*/
public function setUrlScreenshot($urlScreenshot)
{
$this->url_screenshot = $urlScreenshot;
}
/**
* Get url_screenshot
*
* @return string
*/
public function getUrlScreenshot()
{ …Run Code Online (Sandbox Code Playgroud) 我有一个如下定义的类:
[XmlRoot("ClassName")]
public class ClassName_0
{
//stuff...
}
Run Code Online (Sandbox Code Playgroud)
然后我创建一个ClassName_0列表,如下所示:
var myListInstance= new List<ClassName_0>();
Run Code Online (Sandbox Code Playgroud)
这是我用来序列化的代码:
var ser = new XmlSerializer(typeof(List<ClassName_0>));
ser.Serialize(aWriterStream, myListInstance);
Run Code Online (Sandbox Code Playgroud)
这是我用来反序列化的代码:
var ser = new XmlSerializer(typeof(List<ClassName_0>));
var wrapper = ser.Deserialize(new StringReader(xml));
Run Code Online (Sandbox Code Playgroud)
如果我将它序列化为xml,则生成的xml如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfClassName_0 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ClassName_0>
<stuff></stuff>
</ClassName_0>
<ClassName_0>
<stuff></stuff>
</ClassName_0>
</ArrayOfClassName_0>
Run Code Online (Sandbox Code Playgroud)
有没有办法序列化并能够从/向ClassName_0列表反序列化以下内容?
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfClassName xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ClassName>
<stuff></stuff>
</ClassName>
<ClassName>
<stuff></stuff>
</ClassName>
</ArrayOfClassName>
Run Code Online (Sandbox Code Playgroud)
谢谢!
我正在尝试反序列化一个XML字符串,其中元素的值不在我的Enum值的范围内.
Public enum MyEnum
{
Unknown,
Car,
Bicycle,
Boat
}
[SerializableAttribute()]
public class MyClass
{
private string _id;
private MyEnum _myEnum;
public string ID
{
get { return _id; }
set { _id = value; }
}
public MyEnum EnumValue
{
get { return _myEnum; }
set { _myEnum = value; }
}
public MyClass(string id)
{
this._id = id;
}
public MyClass() : this("") { }
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试反序列化后面的字符串(注意Plane作为枚举值):
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><MyClass><ID>1234567890123456789</ID><EnumValue>Plane</EnumValue></MyClass>
Run Code Online (Sandbox Code Playgroud)
然后我的反序列化将抛出异常,甚至在它为EnumValue命中我的公共字段之前,会出现以下异常消息:
实例验证错误:'Plane'不是EnumValue的有效值
是否可以返回EnumValue的默认值,如果我尝试在XML中解析的值不支持EnumValue?例如.对于此处提供的XML字符串,EnumValue应设置为"Unknown".