2011-10-24 7 views
1

XML 계층 구조를 메모리 내 개체 트리로 읽고 싶습니다. XML 트리는 n 레벨의 자식을 가질 수 있습니다. 나는 정확한 숫자를 모른다. 내 메모리 개체에는 자식 및 부모 속성이 트리 컨트롤에 바인딩되어 있습니다.xml 파일/문자열을 구조를 모른 채 일반 방식으로 읽습니다.

xml 요소 태그가 정확히 어떻게 호출되는지/쓰여지는 것을 모르는 경우 어떻게하면 내 메모리 개체로 XML 파일/문자열을 일반적인 방식으로 읽을 수 있습니까?

예를 들어 다른 사람이 각 단위에 많은 단위 등이있는 xml 구조의 단위를 제공 할 수 있습니다. 따라서 xml 태그가 "단위"이지만 모듈 또는 "다른 것"일 수 있습니다. 일반적으로 작동해야하지만 사용자에게 "unit"과 같은 xml 요소 태그 이름을 입력하도록 요청하지 않아도됩니다.

달성하고자하는 일이 모두 가능합니까?

+1

DOM의 개념을 설명하는 것 같습니다. 자식 및 부모 속성이있는 일반적인 메모리 내 객체는 DOM 노드와 똑같이 들립니다. –

+0

내 메모리 객체가 TreeView 컨트롤의 데이터 소스 인 ViewModels입니다. – msfanboy

답변

1

어느 쪽이든을, 당신은 계층 구조를 분석 할 노드 이름을 알고 있어야 적어도 당신은 정의를해야합니다. IMHO, XElement은 범용 XML 파서의 유일한 종류입니다. 앞서 말했듯이

<module name="core"> 
    <modules> 
     <module name="xml"> 
      <modules> 
       <module name="reader" /> 
       <module name="writer" /> 
      </modules> 
     </module> 
     <module name="json"> 
      <modules> 
       <module name="serializer" /> 
       <module name="deserializer" /> 
      </modules> 
     </module> 
    </modules> 
</module> 

, 당신은 root node 같은 몇 가지 정의가 계층 적 요소 이름과 어린이 컨테이너 root node name + s를해야합니다 있어야한다 : 예를 들어,이 같은 XML이있다. 이것은 사용자가 원하는 모든 노드 이름을 지정할 수있는 간단한 방법 중 하나입니다.하지만 제약 조건이 있습니다.

XElement xElement = XElement.Load(@"path\to\your\xml\file"); 
string rootNodeName = xElement.Name.LocalName; 
IEnumerable<XElement> xElements = xElement.Descendants(rootNodeName + "s"); 

을 물론 당신은 xElementsLinq 수 있으며, 당신이 당신의 트리 컨트롤을 구축 재발 할 수있는 계층 구조를 구문 분석 :

는이 같은 사용 XMLXElement 분석 할 수 있습니다.

아래 다음 링크 XElement를에 킥 스타트를 취할 수있다 :이 도움이

희망을.

+1

.Descendants 그리고 당신의 코드는 작동합니다 .--) 나는 당신의 제안을 따를 것입니다. : rootNodeName + "s"); 저를위한 thats oks 지금 나는 생각한다! – msfanboy

1

그냥 XmlDocument에로드 한 다음 XmlNodes을 통과하는 트리를 만듭니다.

+0

@Jakuk 고객 XML 파일과 그 구조를 모르는 경우 각 노드를 읽고 모든 것이 올바른 것으로 가정하는 것이 좋을 것입니다 ... 무엇보다 어디에서 xml 요소 태그 이름을 알 수 있습니까? – msfanboy

+1

@msfanboy - 노드 이름을 알고 있고 그 노드 이름 만 읽으려고하거나 노드 이름을 모르고 전체 파일을 읽길 원합니다. 다른 방법은 없습니다. 당신은'노드가 알려지지 않을 때 XML 파일을 일반적인 방법으로 읽는 법'을 물었다. 다른 대답을 원한다면 다른 질문을 하렴 –

0

를?

using System.Xml ; 
using System.IO; 
class Program 
{ 
    static void Main(string[] args) 
    { 
    using (Stream inputStream = OpenXmlStream()) 
    { 
     XmlDocument document = new XmlDocument() ; 
     document.Load(inputStream) ; 
     Process(document) ; 
    } 
    } 
    static Stream OpenXmlStream() 
    { 
    // provide an input stream for the program 
    } 
    static void Process(XmlDocument document) 
    { 
    // do something useful here 
    } 
} 
1

나는 당신이 성취하고자하는 것을 성취 할 수 있다고 믿습니다. 나는 이런 식으로 뭔가를 할 거라고 :

class GenericNode 
{ 
    private List<GenericNode> _Nodes = new List<GenericNode>(); 
    private List<GenericKeyValue> _Attributes = new List<GenericKeyValue>(); 
    public GenericNode(XElement Element) 
    { 
    this.Name = Element.Name; 
    this._Nodes.AddRange(Element.Elements() 
           .Select(e => New GenericNode(e)); 
    this._Attributes.AddRange(
       Element.Attributes() 
         .Select(a => New GenericKeyValue(a.Key, a.Value)) 
    } 

    public string Name { get; private set; } 
    public IEnumerable<GenericNode> Nodes 
    { 
    get 
    { 
     return this._Nodes; 
    }  
    } 
    public IEnumerable<GenericKeyValue> Attributes 
    { 
    get 
    { 
     return this._Attributes; 
    } 
    } 
} 

class GenericKeyValue 
{ 
    public GenericKeyValue(string Key, string Value) 
    { 
    this.Key = Key; 
    this.Value = Value; 
    } 
    public string Key { get; set; } 
    public string Value { get; set; } 
} 

을 그럼 당신은 단순히 :

XElement rootElement = XElement.Parse(StringOfXml); // or 
XElement rootElement = XElement.Load(FileOfXml); 

GenericNode rootNode = new GenericRode(rootElement); 
+1

나는 당신이'root'를'rootNode'로 바꿀 필요가 있다고 생각합니다. –

관련 문제