你调用的对象是空的
我有以下代码 -
private static void convert()
{
string csv = File.ReadAllText("test.csv");
XDocument doc = ConvertCsvToXML(csv, new[] { "," });
doc.Save("update.xml");
XmlTextReader reader = new XmlTextReader("update.xml");
XmlDocument testDoc = new XmlDocument();
testDoc.Load(@"update.xml");
XDocument turnip = XDocument.Load("update.xml");
webservice.function[] test = new webservice.function[1];
webservice.function CallWebService = new webservice.function();
foreach(XElement el in turnip.Descendants("row"))
{
test[0].com = System.Convert.ToInt32(el.Descendants("var").Where(x => (string)x.Attribute("name") == "com").SingleOrDefault().Attribute("value").Value);
test[0].Centre = el.Descendants("var").Where(x => (string)x.Attribute("name") == "Centre").SingleOrDefault().Attribute("value").Value;
test[0].CCentre = el.Descendants("var").Where(x => (string)x.Attribute("name") == "CCentre").SingleOrDefault().Attribute("value").Value;
MessageBox.Show(test[0].person, "person");
MessageBox.Show(System.Convert.ToString(test[0].actually), "Actually");
MessageBox.Show(System.Convert.ToString(test[0].com), "Com");
CallWebService.updateFeedStatus(test);
}
它出现了错误 - NullReferenceException 未处理,表示对象引用未设置为对象的实例。错误发生在第一行 test[0].account。
我怎样才能克服这个困难?
I have the following code -
private static void convert()
{
string csv = File.ReadAllText("test.csv");
XDocument doc = ConvertCsvToXML(csv, new[] { "," });
doc.Save("update.xml");
XmlTextReader reader = new XmlTextReader("update.xml");
XmlDocument testDoc = new XmlDocument();
testDoc.Load(@"update.xml");
XDocument turnip = XDocument.Load("update.xml");
webservice.function[] test = new webservice.function[1];
webservice.function CallWebService = new webservice.function();
foreach(XElement el in turnip.Descendants("row"))
{
test[0].com = System.Convert.ToInt32(el.Descendants("var").Where(x => (string)x.Attribute("name") == "com").SingleOrDefault().Attribute("value").Value);
test[0].Centre = el.Descendants("var").Where(x => (string)x.Attribute("name") == "Centre").SingleOrDefault().Attribute("value").Value;
test[0].CCentre = el.Descendants("var").Where(x => (string)x.Attribute("name") == "CCentre").SingleOrDefault().Attribute("value").Value;
MessageBox.Show(test[0].person, "person");
MessageBox.Show(System.Convert.ToString(test[0].actually), "Actually");
MessageBox.Show(System.Convert.ToString(test[0].com), "Com");
CallWebService.updateFeedStatus(test);
}
It is coming up with the error of - NullReferenceException was unhandled, saying that the object reference not set to an instance of an object. The error occurs on the first line test[0].account.
How can I get past this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
初始化数组并不会初始化数组中的对象。尝试添加下面的第二行(假设您想使用默认构造函数):
Initializing an array does not initialize the objects in the array. Try adding the second line below (assuming you want to use the default constructor):
(假设 Visual Studio)
(Assuming Visual Studio)
我猜测您的 xml 有一个名称空间,您需要将其包含在 xname 中以获取所需的元素,但正如其他评论提到的,如果不完全公开,几乎无能为力。
I'm guessing your xml has a namespace that you need to include in your xname for the desired elements, but as the other comments mention, there is little that can be done without full disclosure.