내 XML그래서 모든 데이터를 가져 몇 가지 코드가 XML
<?xml version="1.0" encoding="utf-8"?>
<metadata created="2014-05-15T12:26:07.701Z" xmlns="http://site/cu-2.0#" xmlns:ext="http://site/cu/b-2.0">
<customer-list count="47" offset="0">
<customer id="7123456" type="Cust" ext:mark="1">
<name>Tony Watt</name>
<sort-name>Watt, Tony</sort-name>
<gender>male</gender>
<country>US</country>
<knownAs-list>
<knownAs locale="ko" sort-name="Tony Watt"</knownAs>
<knownAs locale="ja" sort-name="Watt Tony"</knownAs>
</knownAs-list>
<tag-list>
<begin>Country</begin>
<tag count="1">
<name>usa</name>
</tag-list>
</customer>
<customer id="9876543" type="Cust" ext:mark="2">
....
</customer-list>
에 중첩 된 요소 Linq에를 가져옵니다. 나는 익명의 유형을 사용하고
이Dim c = From cust As XElement In XDoc.Descendants(ns + "customer")
Select New Customer() With {.Name = cust.Element(ns + "name"),
.Surname = CStr(cust.Element(ns + "surname")),
.Id = cust.Attribute("id"),
.Tag = CStr(cust.Element("tag-list").Element("begin"))}
위의 코드는 XML 데이터를 반환하지만 코드
.Tag = CStr(cust.Element("tag-list").Element("begin"))
이 라인을 추가하면 예외가 발생합니다 아래로 클래스에 값을 추가 한 단계 더 나아가 , "개체 참조가 개체의 인스턴스로 설정되지 않았습니다." 지금 여기에 두 가지 가능성을 프로그래머
- I합니다 ('태그 목록'요소에서 검색 '시작'하는) 특정 라인에 대한 잘못된 내 코드
- 내가 어떤 태그 목록 요소가 시작 중첩 해달라고 알고있다 요소가 있으므로 혼란이 생길 수 있습니다. 이것을 극복하기 위해 Cstr을 추가했는데 이것이 충분한 지 확실하지 않은가요?
MSDN을 읽은 후 .Descendants (Xdoc.Descendants)는 모든 요소에서 모든 데이터를 가져올 것입니다. 여기에서 요소는 내가 설명한 경로까지 데이터를 반환 할 것이므로 데이터를 ' 위의 코드를 사용할 수 있어야합니다. 누구든지 태그 목록에서 시작 데이터를 얻는 데 도움이 될 수 있습니까?
태그 요소가 유효합니까? 나는 거기에 닫는 태그를 볼 수 없다. – dotnetom
네, 죄송합니다. 나는 내가 지금 바꾼 XML을 올바르게 추가/포맷하지 않았다는 것을 알았습니다. – Computer
XPath를 사용하는 것이 좋습니다. http://stackoverflow.com/questions/3642829/ how-to-use-xpath-with-xelement-or-linq – Neolisk