Linq to XML一般错误 [英] Linq to XML general error
本文介绍了Linq to XML一般错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从以下代码中收到一条非常奇怪的(常规)错误消息...
公共客户作为列表( 客户)= Nothing
公共 sFileName 作为 字符串 = C:\ Users \ sean \Documents\CustDB.xml
Sub LoadXmlData(sFileName As 字符串)
Dim xDoc 作为 XDocument = Nothing
Dim custList As IEnumerable = 没有跨度>
尝试
Customers = 新列表( Of Customer)
xDoc = XDocument.Load(sFileName,LoadOptions.PreserveWhitespace)
custList =来自客户在 xDoc中.Elements( CustDB)。元素( 客户)_
按客户排序。价值升序_
选择 Nm = customer.Elements( 名称)。值,
Ad1 =客户。元素( Street)。值,
Ad2 = customer.Elements( CityStateZip)。值,
Ph = customer.Elements( Phone)。值,
Fx = customer.Elements( Fax )。值,
Ce = customer.Elements( Cell) .Value,
Oth = customer.Elements( Other)。价值,
Atn = customer.Elements( Attn)。价值,
Eml =客户.Elements( Email)。价值,
SaPe = customer.Elements( SalesPerso n)。值,
AcTy = customer.Elements( AcctType)。值
对于 每个 cl 在 custList
Dim cu As Customer = 新客户
使用 cu
.CustName = cl.Nm
.Street = cl .Ad1
.CityStateZip = cl.Ad2
.Phone = cl.Ph
.Fax = cl.Fx
.Cell = cl.Ce
。其他= cl .Oth
.Attn = cl.Atn
.Email = cl.Eml
.SalesPerson = cl.SaPe
.AcctType = cl.AcTy
结束 使用
Customers.Add(cu)
下一步
Catch ex As 异常
MsgBox( ModMain错误:& ex.Message,MsgBoxStyle.Exclamation, 错误...)
最后
xDoc = 没什么
结束 尝试
结束 Sub
XML的结构如下......
<? xml version = 1.0 编码 = utf-8? >
< CustDB >
< 客户 >
< 名称 > A& T Window Cleaning < /姓名 >
< 街道 > < / Street >
< CityStateZip > < / CityStateZip >
< 电话 > < /电话 >
< 传真 > < ; / Fax >
< Cell > < / Cell >
< 其他 > < /其他 >
< Attn > Troy < / Attn >
< 电子邮件 > < /电子邮件 >
< SalesPerson > < / SalesPerson >
< AcctType > < / AcctType >
< /客户 > ;
< 客户 >
< 名称 > Adventure Chrisitan Church < / Name >
< 街道 > < / Street >
< CityStateZip > < / CityStateZip >
< 电话 > < /电话 >
< 传真 > < /传真 >
< Cell > < / Cell >
< 其他 > < /其他 >
< Attn > Richard Mosqueda < < span class =code-leadattribute> / Attn >
< 电子邮件 > < /电子邮件 >
< SalesPerson > < / SalesPerson >
< < span class =code-leadattribute> AcctType > < / AcctType >
< /客户 >
...
< / CustDB >
可以在此处找到错误的屏幕截图。
Meicji(我认为我拼写正确)时我唯一改变的就是在XML和解析中添加'AcctType'。
解决方案
替换:
<?xml version = 1.0 encoding = utf-8?>
with:
<?xml version =1.0encoding =utf-8?>
你看到了区别吗?
见:
HTTP://www.w3 schools.com/xml/ [ ^ ]
I get a very strange (general) error message from the following code...
Public Customers As List(Of Customer) = Nothing
Public sFileName As String = "C:\Users\sean\Documents\CustDB.xml"
Sub LoadXmlData(sFileName As String)
Dim xDoc As XDocument = Nothing
Dim custList As IEnumerable = Nothing
Try
Customers = New List(Of Customer)
xDoc = XDocument.Load(sFileName, LoadOptions.PreserveWhitespace)
custList = From customer In xDoc.Elements("CustDB").Elements("Customer") _
Order By customer.Value Ascending _
Select Nm = customer.Elements("Name").Value,
Ad1 = customer.Elements("Street").Value,
Ad2 = customer.Elements("CityStateZip").Value,
Ph = customer.Elements("Phone").Value,
Fx = customer.Elements("Fax").Value,
Ce = customer.Elements("Cell").Value,
Oth = customer.Elements("Other").Value,
Atn = customer.Elements("Attn").Value,
Eml = customer.Elements("Email").Value,
SaPe = customer.Elements("SalesPerson").Value,
AcTy = customer.Elements("AcctType").Value
For Each cl In custList
Dim cu As Customer = New Customer
With cu
.CustName = cl.Nm
.Street = cl.Ad1
.CityStateZip = cl.Ad2
.Phone = cl.Ph
.Fax = cl.Fx
.Cell = cl.Ce
.Other = cl.Oth
.Attn = cl.Atn
.Email = cl.Eml
.SalesPerson = cl.SaPe
.AcctType = cl.AcTy
End With
Customers.Add(cu)
Next
Catch ex As Exception
MsgBox("ModMain error: " & ex.Message, MsgBoxStyle.Exclamation, "Error...")
Finally
xDoc = Nothing
End Try
End Sub
The XML is structured like this...
<?xml version=1.0 encoding=utf-8?>
<CustDB>
<Customer>
<Name>A&T Window Cleaning</Name>
<Street></Street>
<CityStateZip></CityStateZip>
<Phone></Phone>
<Fax></Fax>
<Cell></Cell>
<Other></Other>
<Attn>Troy</Attn>
<Email></Email>
<SalesPerson></SalesPerson>
<AcctType></AcctType>
</Customer>
<Customer>
<Name>Adventure Chrisitan Church</Name>
<Street></Street>
<CityStateZip></CityStateZip>
<Phone></Phone>
<Fax></Fax>
<Cell></Cell>
<Other></Other>
<Attn>Richard Mosqueda</Attn>
<Email></Email>
<SalesPerson></SalesPerson>
<AcctType></AcctType>
</Customer>
...
</CustDB>
The screen shot of the error can be found here.
The only thing I changed from when Meicji (I think I spelled that right) helped me with this is the addition of the 'AcctType' in the XML and parsing.
解决方案
Replace:
<?xml version=1.0 encoding=utf-8?>
with:
<?xml version="1.0" encoding="utf-8"?>
Do you see the difference?
See:
http://www.w3schools.com/xml/[^]
这篇关于Linq to XML一般错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文