2012-10-31 2 views
-1

나는 C#을 아주 새로운 해요, 이것은 아마 매우 멍청한 놈 간단한 질문이다, 나는 문제가 무엇인지 설명하기 위해 최선을 다할 것입니다 : 나는에 어려움을 겪고 있어요참조하는 다른 클래스의 객체 및 변수는

클래스 간의 "참조"변수 ... 상황을 설명하고 내가해야 할 일을 설명합시다 :

"Name", "Length", XCoordinate와 같은 변수 힙을 포함하는 "Node"클래스가 있습니다. ','YCoordinate ','Node Type '- XML ​​파일에서 읽는 모든 문자열입니다.

또한 "Length", 'TrackType'등의 문자열 변수가 포함 된 "NodeLinkage"클래스가 있습니다.

문제점 : XML 파일에서이 데이터를 수집 중이며 두 개의 텍스트 파일 (Node 용, 하나는 Links (NodeLinkage 용))을 작성해야합니다. NodeLinkage 파일은 노드 파일에있는 동일한 정보 중 일부 (예 : "NodeAName"및 "NodeBName"- 두 개의 'Node'객체 이름)를 가져야합니다. 나는이 특정 노드를 참조 할 각 'NodeLinkage'에서 '포인터'(또는 비슷한 것)를 사용할 수있는 몇 가지 방법이 있기를 바라고 있습니다. 따라서 이름 (및 필요한 다른 변수)을 훨씬 쉽게 저장할 수 있습니다 ...

이 XML 파일에는 최대 5000 개의 노드가 있으며 많은 수의 링크가 있으며 XML 파일이 읽혀 노드 데이터가 먼저 읽힌 다음 링크 데이터가 읽혀집니다.

읽기 (목록 /지도/사전)시이 정보 (노드 및 노드 링크)를 저장하는 가장 좋은 방법은 무엇이며 '포인터'와 같은 것을 사용하여 링크의 특정 노드/노드 이름을 참조하는 방법은 무엇입니까?


편집! : 지금까지 대답 한 사람들에게

감사합니다! 노드/링크 기록의 각은 약 15 ~ 20 속성이

<Nodes> 
    <Node Name="WAI2" LongName="Waitara" Length="100" NodeType="6" ... (other attributes)> 
     <NodeMasterTimingPoint NodeName="WAI1"></NodeMasterTimingPoint> 
    </Node> 
    <Node Name="WAI3" LongName="Waitara" Length="100" NodeType="6" ... (other attributes)> 
     <NodeMasterTimingPoint NodeName="WAI1"></NodeMasterTimingPoint> 
    </Node> 
    .... other nodes 
</Nodes> 

<Links> 
    <Link NodeAName="WAI1" NodeBName="WAI2" Length="200" TrackType="37" SBId="10482"> 
    </Link> 
    ... more links 
</Links> 

: 여기에 읽고있는 XML 파일의 샘플입니다.

링크 레코드는 두 개의 NodeXName을 사용합니다.이 두 노드를 참조하거나 '가리키는'방법이 있는지 궁금 해서요. 따라서 NodeA.Name과 같은 링크 데이터를 인쇄 할 때 궁금합니다. 사용 될수있다. 따라서 필요한 경우 NodeA.Length와 같은 다른 데이터를 사용할 수도 있습니다.

Node Class: 

class cNode 
{ 
    public string Name; 
    public string LongName; 
    public string PlatformName; 
    public string NodeType; 
    public string Length; 
    public string MasterTimingNode; 
    public string MasterJunctionNode; 
    public string[] PenaltyFromNode; 
    public string[] PenaltyToNode; 
    public string[] Penalty; 
    public string PFMORI; 
    public string XCRD; 
    public string YCRD; 
    public string PlanLocation; 

    // Few methods which aren't important 
    public string GetDataString() 
    { 
    ... 
    } 

} 

NodeLinkage 클래스는 위 Node 클래스와 매우 유사합니다.

나는 main 파일을 가지고 "XML"파일을 읽고 각 노드/링크에 값을 할당하는 "main"클래스를 가지고있다. 명부? 지도? 링크 데이터도 판독하기 전에 전술 한 바와 같이

은, 노드 데이터를 판독 및 저장된다 (따라서, 노드 오브젝트가 작성) ..

감사 다시 닉.

읽기에는 엄청난 양의 물건이 필요합니다.: 당신이 그런 모든 관련 노드 정보를 사용할 수있을 것이다 당신이 당신의 XML 파일

를 읽을 때 당신의 NodeLinkage 클래스 유형 '노드'의 변수를 추가 한 다음 그것을 할당 할 것처럼 P

+0

클래스/xml 구조를 보려면 여기를 클릭하면 도움이 될 것이라고 생각합니다. – caesay

+0

위에 추가되었습니다. 고마워요 – Nick

답변

0

는 소리 해당 노드에 대한 참조를 통한 NodeLinkage 클래스

노드 연결이 얼마나 많은 노드를 참조 할 수 있는지에 따라 달라 지겠지만, 몇 가지 참조를 하드 코딩 할 수 있습니다.

class NodeLinkage 
{ 
    public Node NodeA { get; set; } 
    public Node NobeB { get; set; } 
} 

또는 당신이 지금까지 당신에게 유용한

List<Node> Nodes = new List<Node>(); 
// Read node data 
Nodes = SomeRoutineThatGetsThem(); 

List<NodeLinkage> Links = new List<NodeLinkage>(); 
// Read link data 
Links = SomeRoutineThatGetsThem2(); 

// Create node linkage refs 
foreach(var link in Links) 
{ 
    // Find the nodes that should be associated with this link - maybe using Linq? 
    var assocNodes = Nodes.Where(n => n.NodeID == link.NodeID); // No idea what your setup is so this is a random pseudo code bit! 
    // blah - assign etc 
} 

목록 노드를 구축 할 필요가 같은

class NodeLinkage 
{ 
    public List<Node> Nodes { get; set; } 
} 

은 또한 소리 DICT/목록을 잡아?

+0

우리는 그 indepth 가서 충분한 정보가 정말로 없다. – caesay

+0

모든 노드 데이터를 먼저 읽었 기 때문에 나는 그렇게 생각하지 않습니다. 따라서 NodeLinkage 객체가 제대로되기 전에 노드를 만들어야합니까? ... 어쨌든 더 많은 정보가 원래 게시물에 추가되었습니다. (XML 파일과 Node 클래스)가 환호합니다. – Nick

0

"NodeType"은 "노드"의 속성입니다. 그러나 "Node"와 "NodeLinkage"사이의 관계가 무엇인지는 명확하지 않습니다. 나는이 NodeLinkage가 실제로 직렬화/역 직렬화 중에 객체 간의 관계를 다루는 방법 일 뿐이라고 의심한다. (그 단어가 아직 설명되지 않은 경우 직렬화는 메모리에 객체 세트를 가져 와서 파일 및/또는 데이터베이스, deserialization은 파일 및/또는 데이터베이스에서 개체 집합을 만드는 것입니다).

메모리의 개체 ("개체 인스턴스")는 다른 개체에 대한 참조를 갖습니다. 예를 들어 "노드"에는 하나의 NodeType 만 있거나 여러 개의 노드 유형이있을 수 있습니다. 어느 쪽이든, 노드의 인스턴스에 관한 한 NodeType (또는 NodeTypes)은 단순히 다른 객체에 대한 참조 일뿐입니다. 하나 또는 수백 개의 노드가 모두 동일한 노드 유형을 공유하는지는 중요하지 않습니다. 그들은 모두 동일한 NodeType 인스턴스를 가리킬 것입니다. 100 개의 NodeType 인스턴스가있는 100 개의 노드가 없습니다.

그러나 NodeType 항목이 중복되는 것을 피하기 위해 serialize 할 때가되면 식별자가있는 NodeType을 추상화 할 수 있기를 원할 것입니다 (종종 Identity 속성이라고합니다). 우리는 솔루션에 도착하기 전에, 우리의 클래스 구조를 살펴 보자 그들이 직렬화 할 방법이 개체 모델에서

public class NodeManager 
    { 
     public List<Node> Nodes { get; set; } 
    } 

    public class Node 
    { 
     public string Name { get; set; } 
     public List<NodeType> NodeTypes{ get; set; } 
     public int Length { get; set; } 
     public int XCoordinate { get; set; } 
     public int YCoordinate { get; set; } 
    } 

    public class NodeType 
    { 
     public string Name { get; set; } 
     public string SomeOtherNodeTypeProperty { get; set; } 
     public string YetAnotherNodeTypeProperty { get; set; } 
    } 

의 NodeManager 클래스는이 모든 노드에 대한 메모리 저장소의 역할 인스턴스화되었습니다. 이 클래스는 직렬화 또는 비 직렬화 할 클래스입니다. 여기에는 노드 모음이 포함되어 있습니다. 더 자세한 모델에서, 아마 생성, 추가, 삭제 및 유용한 방법으로 노드 집합을 쿼리하는 방법을 포함하지만, 논의 버리는/deser의 목적이 충분하다.

보시다시피 Node 객체에는 여러 유형이있을 수 있습니다. 우리가 메모리 만 개 노드, 몇 가지 유형 각각 만들 수 있다면 시리얼 라이저가 있거나 노드에 대한 참조로 추상화 이는 NodeType 인스턴스에 대한 방법이 없기 때문에, 우리는 기록 된 파일의 노드 종류의 XML 노드의 대규모 중복 것 . 우리의 XML 파일은 다음과 같이 보일 것 (하지 정확하게 같은을, 나는이 한 손으로) : 두 노드 각각 지원 3 노드 유형, 그리고 서로의 완전한 중복 얼마나

<?xml version="1.0" encoding="utf-8"?> 
<Nodes> 
    <Node> 
     <Name>Foo</Name> 
     <Length>5</Length> 
     <XCoordinate>24</XCoordinate> 
     <YCoordinate>36</YCoordinate> 
     <NodeTypes> 
      <NodeType> 
       <Name>Type 1</Name> 
       <SomeOtherNodeTypeProperty>Blah</SomeOtherNodeTypeProperty> 
       <YetAnotherNodeTypeProperty>Meh</YetAnotherNodeTypeProperty> 
      </NodeType> 
      <NodeType> 
       <Name>Type 2</Name> 
       <SomeOtherNodeTypeProperty>Foo</SomeOtherNodeTypeProperty> 
       <YetAnotherNodeTypeProperty>Bar</YetAnotherNodeTypeProperty> 
      </NodeType> 
      <NodeType> 
       <Name>Type 3</Name> 
       <SomeOtherNodeTypeProperty>Fizz</SomeOtherNodeTypeProperty> 
       <YetAnotherNodeTypeProperty>Buzz</YetAnotherNodeTypeProperty> 
      </NodeType> 
     </NodeTypes> 
    </Node> 
    <Name>Bar</Name> 
    <Length>15</Length> 
    <XCoordinate>224</XCoordinate> 
    <YCoordinate>336</YCoordinate> 
    <NodeTypes> 
     <NodeType> 
      <Name>Type 1</Name> 
      <SomeOtherNodeTypeProperty>Blah</SomeOtherNodeTypeProperty> 
      <YetAnotherNodeTypeProperty>Meh</YetAnotherNodeTypeProperty> 
     </NodeType> 
     <NodeType> 
      <Name>Type 2</Name> 
      <SomeOtherNodeTypeProperty>Foo</SomeOtherNodeTypeProperty> 
      <YetAnotherNodeTypeProperty>Bar</YetAnotherNodeTypeProperty> 
     </NodeType> 
     <NodeType> 
      <Name>Type 3</Name> 
      <SomeOtherNodeTypeProperty>Fizz</SomeOtherNodeTypeProperty> 
      <YetAnotherNodeTypeProperty>Buzz</YetAnotherNodeTypeProperty> 
     </NodeType> 
    </NodeTypes> 
    </Node> 
</Nodes> 

공지 사항. 이를 방지하기 위해 NodeTypeID 속성을 추가 할 수 있습니다. 이것은 단순히 모든 NodeType 인스턴스에서 고유해야하는 값입니다. 우리가 대신 전체 노드 종류를 작성할 필요없이, 우리가 쓸 수 있다고이있는 경우는 ID, 그리고 아이디에서 우리는 노드 종류에 대한 정보의 나머지 얻을 수 있습니다.의 우리 있거나 노드에 새 속성을 추가하자이 그렇게 꽤 긴지고

<?xml version="1.0" encoding="utf-8"?> 
<NodeRoot> 
    <Nodes> 
     <Node> 
      <Name>Foo</Name> 
      <Length>5</Length> 
      <XCoordinate>24</XCoordinate> 
      <YCoordinate>36</YCoordinate> 
      <NodeTypes> 
       <NodeType NodeTypeId="1"/> 
       <NodeType NodeTypeId="2"/> 
       <NodeType NodeTypeId="3"/> 
      </NodeTypes> 
     </Node> 
     <Node> 
      <Name>Bar</Name> 
      <Length>15</Length> 
      <XCoordinate>224</XCoordinate> 
      <YCoordinate>336</YCoordinate> 
      <NodeTypes> 
       <NodeType NodeTypeId="1"/> 
       <NodeType NodeTypeId="2"/> 
       <NodeType NodeTypeId="3"/> 
      </NodeTypes> 
     </Node> 
    </Nodes> 
    <NodeTypes> 
     <NodeType> 
      <Name>Type 1</Name> 
      <SomeOtherNodeTypeProperty>Blah</SomeOtherNodeTypeProperty> 
      <YetAnotherNodeTypeProperty>Meh</YetAnotherNodeTypeProperty> 
     </NodeType> 
     <NodeType> 
      <Name>Type 2</Name> 
      <SomeOtherNodeTypeProperty>Foo</SomeOtherNodeTypeProperty> 
      <YetAnotherNodeTypeProperty>Bar</YetAnotherNodeTypeProperty> 
     </NodeType> 
     <NodeType> 
      <Name>Type 3</Name> 
      <SomeOtherNodeTypeProperty>Fizz</SomeOtherNodeTypeProperty> 
      <YetAnotherNodeTypeProperty>Buzz</YetAnotherNodeTypeProperty> 
     </NodeType> 
    </NodeTypes> 
</NodeRoot> 

나 ': 이제 우리는 우리의 노드를 직렬화 NodeTypeID 속성이 있음을

public class NodeType 
{ 
    public int NodeTypeId { get; set; } 
    public string Name { get; set; } 
    public string SomeOtherNodeTypeProperty { get; set; } 
    public string YetAnotherNodeTypeProperty { get; set; } 
} 

, 그것은 많은 청소기를 보이는

여기에서 그것을 감쌀 것이다. 잘하면 당신이 찾고있는 방향을 알려 줬습니다.

관련 문제