我有以下反序列化XML的方法:
公共类XMLObjects{public static T ConvertXmlToClass(字符串xml){
xml = "";
xml += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
xml += "<root>";
xml += " <success>true</success>";
xml += " <data>";
xml += " <item>";
xml += " <Barcode>20450000817386</Barcode>";
xml += " <StateId>1</StateId>";
xml += " <WareReceiverId />";
xml += " </item>";
xml += " </data>";
xml += " <errors />";
xml += " <warnings />";
xml += " <info />";
xml += "</root>";
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "root";
xRoot.IsNullable = true;
var serializer = new XmlSerializer(typeof(T), xRoot);
return (T)serializer.Deserialize(new StringReader(xml));
}
}
这是对象类:
[Table("DocumentsTracking", Schema = "np")]
public partial class DocumentsTracking
{
[Key]
[XmlElement("Barcode")]
public string Barcode { get; set; }
[XmlElement("StateId")]
public Nullable<int> StateId { get; set; }
[XmlElement("WareReceiverId")]
public Nullable<int> WareReceiverId { get; set; }
}
我得到以下例外情况:
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=There is an error in XML document (1, 524).
Source=System.Xml
StackTrace:
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
at Iwatch.Accounting.Data.XMLObjects.ConvertXmlToClass[T](String xml) in f:\proj\Iwatch.Accounting.Data\XMLObjects.cs:line 71
at Iwatch.Accounting.Data.Logic.XmlDataLogic`1.UpdateRecords(String xml) in f:\proj\Iwatch.Accounting.Data.Logic\XmlDataLogic.cs:line 15
at Iwatch.Accounting.Data.Logic.XmlDataLogic`1.RefreshAllByStringList(List`1 list) in f:\proj\Iwatch.Accounting.Data.Logic\XmlDataLogic.cs:line 56
at Iwatch.Accounting.UI.MainForm.button1_Click(Object sender, EventArgs e) in f:\proj\Iwatch.Accounting.UI\MainForm.cs:line 45
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Iwatch.Accounting.UI.Program.Main() in f:\proj\Iwatch.Accounting.UI\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.FormatException
HResult=-2146233033
Message=Input string was not in a correct format.
Source=mscorlib
StackTrace:
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Xml.XmlConvert.ToInt32(String s)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderXmlRoot1.Read1_NullableOfInt32(Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderXmlRoot1.Read13_DocumentsTracking(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderXmlRoot1.Read14_XmlRootDataOfDocumentsTracking(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderXmlRoot1.Read15_Item(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderXmlRoot1.Read16_root()
InnerException:
如何正确设置getter和setter?或者也许有其他方法可以解决这个问题?我试着这样做,它是工作,但有没有更好的办法?
[XmlIgnore]
public Nullable<int> DeliveryForm { get; set; }
[NotMapped]
[XmlElement("DeliveryForm")]
public string DeliveryFormString
{
get
{
return this.DeliveryForm != null ? this.DeliveryForm.Value.ToString() : "";
}
set
{
this.DeliveryForm = (value.Equals("") ? 0 : Int32.Parse(value));
}
}
试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
//string xml = "";
//xml += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
//xml += "<root>";
//xml += " <success>true</success>";
//xml += " <data>";
//xml += " <item>";
//xml += " <Barcode>20450000817386</Barcode>";
//xml += " <StateId>1</StateId>";
//xml += " <WareReceiverId />";
//xml += " </item>";
//xml += " </data>";
//xml += " <errors />";
//xml += " <warnings />";
//xml += " <info />";
//xml += "</root>";
Root root = new Root()
{
success = true,
data = new Data()
{
item = new Item()
{
Barcode = "20450000817386",
StateId = 1,
WareReceiverId = null
},
},
errors = string.Empty,
warnings = string.Empty
};
XmlSerializer serializer = new XmlSerializer(typeof(Root));
StreamWriter writer = new StreamWriter(FILENAME);
serializer.Serialize(writer, root);
writer.Flush();
writer.Close();
writer.Dispose();
XmlSerializer xs = new XmlSerializer(typeof(Root));
XmlTextReader reader = new XmlTextReader(FILENAME);
Root newRoot = (Root)xs.Deserialize(reader);
}
}
[XmlRoot("root")]
public partial class Root
{
[XmlElement("success")]
public Boolean success { get; set; }
[XmlElement("data")]
public Data data { get; set; }
[XmlElement("errors")]
public string errors { get; set; }
[XmlElement("warnings")]
public string warnings { get; set; }
}
[XmlRoot("data")]
public partial class Data
{
[XmlElement("item")]
public Item item { get; set; }
}
[XmlRoot("item")]
public partial class Item
{
[XmlElement("Barcode")]
public string Barcode { get; set; }
[XmlElement("StateId")]
public Nullable<int> StateId { get; set; }
[XmlElement("WareReceiverId")]
public int? WareReceiverId { get; set; }
}
}
我是C#的新手,我有一些Java方面的基础知识,但我不能让这段代码正常运行。 它只是一个基本的计算器,但当我运行程序时,VS2008给了我这个错误: 我做了几乎相同的程序,但在Java使用的是JSwing,它运行得非常好。 下面是C#的形式: 会有什么问题?有没有办法解决? PS:我也试过 但没有奏效。
我正在尝试反序列化以 XML soap 格式接收的消息。以前这一直在工作,但由于源消息的更改,我现在遇到以下问题 我可以看到这是因为消息中的以下内容.... 我认为问题在于它试图将一个字符串数据类型赋值或“强制转换”到一个空字段上,因此抛出一个错误,指出该字段是一个对象,不能被识别为字符串。 我的问题是如何阻止反序列化程序首先尝试读取这个特定的空字段。这是我在使用Jackson的Java代码中声明
问题内容: 我有一个WCF服务定义如下: 所述类的定义为: ILayoutService的实现如下: 我实现了,所以我可以得到将被(反)序列化的原义JSON。我正在使用它来测试通过jQuery调用此服务: 返回的布局是VB代码中调用返回的确切JSON 。出于某种原因,当我调用Web服务,输入参数在什么都不是。反序列化一定会失败。知道为什么吗? 问题答案: 仅凭您提供的细节很难分辨。 这是我的建议:
有没有一种方法可以正确地反序列化以双引号开头和结尾的JSON字符串? 数据以流的形式输入(来自AWS S3对象)。这是流的内容(注意开头和结尾的双引号字符): JsonSerializer似乎没有正确地反序列化它。但是,对于以下内容,其工作原理与预期一致: 以下是片段:
问题内容: 我正在尝试格式化字符串,以便所有内容在两者之间对齐。 我正在尝试这样做: 我如何获得要排队的列?我看了文档但是很困惑。我当时以为它将使它成为30个空格,然后它将打印下一个项目,但是似乎从上一个项目结束处开始有30个空格。 谢谢 问题答案: 使您的字段在可用空间内保持对齐。使用对齐方式说明符更改对齐方式: 强制字段在可用空间内左对齐(这是大多数对象的默认设置)。 强制字段在可用空间内右对
问题内容: 我有以下类,它是由Jackson映射的(简化版): 在某些情况下,服务器会返回,然后我想将name设置为空的Java String。 是否有任何Jackson注释,或者如果属性为,我应该只检查getter中的null并返回空字符串? 问题答案: 您可以在默认构造函数中或声明时进行设置: 要么
我有以下由Jackson映射的类(简化版): 在某些情况下,服务器返回,然后我想将name设置为空Java字符串。 是否有任何Jackson注释,或者如果属性为,我应该只检查getter中的null并返回空字符串吗?
问题内容: 我正在处理一个包含坐标x,y,z的文本文件 每行使用 处理完数据后,我需要将坐标写回到另一个txt文件中,以便使每一列中的项目(以及输入文件)都正确对齐。每行由坐标组成 C ++中是否有任何类似操纵器的控件可以设置宽度和对齐方式? 问题答案: 使用更新的语法尝试这种方法: 以下是使用旧语法(对不支持的旧版Python有用)的方法: