다음과 같은 XML 스키마가 있다고 가정 해 보겠습니다.구조체에 직접 저장하는 LINQ를 사용하는 C# XML 구문 분석
<root>
<version>2.0</version>
<type>fiction</type>
<chapters>
<chapter>1</chapter>
<title>blah blah</title>
</chapter>
<chapters>
<chapter>2</chapter>
<title>blah blah</title>
</chapters>
</root>
XML에서 반복되지 않고 LINQ를 사용하여 구조체에 직접 저장하는 요소를 파싱 할 가능성이 있습니까?
예를 들어 "version"과 "type"에 대해 이렇게 할 수 있습니까?
//setup structs
Book book = new Book();
book.chapter = new Chapter();
//use LINQ to parse the xml
var bookData = from b in xmlDoc.Decendants("root")
select new
{
book.version = b.Element("version").Value,
book.type = b.Element("type").Value
};
//then for "chapters" since I know there are multiple I can do:
var chapterData = from c in xmlDoc.Decendants("root"
select new
{
chapter = c.Element("chapters")
};
foreach (var ch in chapterData)
{
book.chapter.Add(getChapterData(ch.chapter));
}
변경 가능한 구조체를 사용하지 마십시오. –
왜 확장 할 수 있습니까? 나는 코딩 실력을 향상시키기 위해 모든 노력을 기울이고 있습니다. – Luke
악의적 인 변경 가능한 구조체에 대한 기본 개요 : http://stackoverflow.com/questions/441309/why-are-mutable-structs-evil –