引用其他类对象和变量

时间:2012-10-31 00:14:11

标签: c# xml pointers reference

我对c#很新,这可能是一个非常简单的问题,我会尽力解释问题所在:

我正努力在类之间“引用”变量......让我解释一下情况以及我需要做的事情:

我有一个“Node”类,它包含一堆变量,如'Name','Length',XCoordinate','YCoordinate','Node Type' - 它们都是我从XML读取的字符串文件。

我还有一个“NodeLinkage”类,其中包含一堆字符串变量(也可以从XML文件中读取),例如“Length”,“TrackType”等等等等。

问题:我从XML文件中收集这些数据,并且需要编写两个文本文件(一个用于Node,一个用于Links(NodeLinkage))。 NodeLinkage文件还需要具有Node文件中的一些相同信息(例如“NodeAName”和“NodeBName” - 两个“Node”对象名称)。我希望有一些方法可以在每个'NodeLinkage'中使用引用这些特定节点的'指针'(或类似的东西)......因此更容易存储名称(以及所需的任何其他变量)...... / p>

这些XML文件最多包含5000个节点和大量链接,并且读取XML文件,以便首先读取节点数据,然后读取链接数据。

读取时(Lists / map / dictionary)存储此信息(节点和NodeLinkages)的最佳方法是什么?如何使用“指针”之类的东西来引用链接中的特定节点/ NodeName。


编辑:!

谢谢那些到目前为止回复的人!以下是正在阅读的XML文件示例:

<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>

每个节点/链接记录都有大约15-20个属性。

正如你所看到的,Link记录使用了两个'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方法的“main”类,它只是读取XML文件并为每个Node / Links分配值 - 不确定如何存储每个节点/链接?名单?地图?

如前所述,在读取链接数据之前,会读取并存储节点数据(因此会创建节点对象)。

再次感谢, 尼克。

阅读大量的东西。 :P

2 个答案:

答案 0 :(得分:0)

听起来你只想在NodeLinkage类中添加一个'Node'类型的变量,然后在读取XML文件时分配它

然后,所有相关节点信息将通过对相应节点的引用

提供给NodeLinkage类

我认为这取决于节点链接可以引用多少节点,你可以硬编码几个引用,例如。

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

或者持有一个词典/列表

class NodeLinkage 
{
    public List<Node> Nodes { 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
}

到目前为止这对你有用吗?

答案 1 :(得分:0)

据我所知,“NodeType”将是“节点”的属性。但是,不清楚“节点”和“节点链接”之间的关系是什么。我怀疑这个NodeLinkage实际上只是处理序列化/反序列化过程中对象之间关系的一种方式(如果还没有向你解释这些单词,序列化就是在内存中获取一组对象并将它们持久化的过程文件和/或数据库;反序列化是从文件和/或数据库创建一组对象。

内存中的对象(“对象实例”)将引用其他对象;例如,您的“节点”可能只有一个NodeType,或者它可能有多个NodeType。无论哪种方式,就您的Node实例而言,NodeType(或NodeTypes)只是对另一个对象的引用。一个或一百个节点是否共享相同的节点类型无关紧要;它们都将指向相同的NodeType实例 - 您将不会有一百个具有一百个NodeType实例的节点。

但是,当序列化时,为了避免重复一堆NodeType条目,您将希望能够使用标识符抽象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类充当已实例化的所有节点的内存存储库。这是您将序列化或反序列化的类。它包含一组节点,没有别的。在更详细的模型中,它可能包含以有用的方式创建,添加,删除和查询Node集合的方法,但是为了讨论ser / deser,这已经足够了。

如您所见,Node对象可以有多种类型。如果我们要在内存中创建一万个节点,每个节点都有几种类型,我们就会在写入的文件中大量复制NodeType XML节点,因为序列化程序无法将NodeType实例抽象为对NodeType的引用。我们的XML文件看起来像这样(不完全像这样;我手工完成):

<?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>

注意两个节点如何支持3个节点类型,并且它们是彼此完全重复的。为避免这种情况,我们可以添加NodeTypeID属性。这只是一个保证在所有NodeType实例中都是唯一的值。当我们拥有它时,我们可以只写出它的ID,而不必写出整个NodeType,从ID我们可以获得有关NodeType的其他信息。让我们为NodeType添加一个新属性:

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

现在我们序列化节点时有一个NodeTypeID属性,它看起来更清晰:

<?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>

这已经很久了,所以我将它包装在这里;希望我已经给你了你正在寻找的方向。