C# 数组索引的XML问题

C# 数组索引的XML问题,c#,xml,arrays,C#,Xml,Arrays,一切正常,但问题是PortName和PortId都是字符串数组变量。不知道如何为每次测试迭代获取索引动态值。对我来说,每次都只获取零索引值。为什么需要测试数组?您应该使用PortName1数组或PortId1,如下所示: int[] test = new int[3]; string[] PortName1 = new string[3]; string[] PortId1 = new string[3]; PortName1[0]="portname1"; PortName1[1] = "po

一切正常,但问题是PortName和PortId都是字符串数组变量。不知道如何为每次测试迭代获取索引动态值。对我来说,每次都只获取零索引值。

为什么需要测试数组?您应该使用PortName1数组或PortId1,如下所示:

int[] test = new int[3];
string[] PortName1 = new string[3];
string[] PortId1 = new string[3];
PortName1[0]="portname1";
PortName1[1] = "portname2";
PortName1[2] = "portname3";

PortId1[0] = "port1";
PortId1[1] = "port2";
PortId1[2] = "port3";
var fileName = @"C:\sample.xml";
var xdoc = XDocument.Load(fileName);

var server = 
    new XElement("server",
        new XAttribute("serverid", ServerId),
        new XAttribute("name", ServerName),
            test.Select(sample =>
                new XElement("port",
                    new XAttribute("asset", PortName),
                    new XAttribute("portid", PortId)
                ))
        );
var portIdIndex = 0; // declare variable somewhere before

PortName1.Select(port =>
             new XElement("port",
                 new XAttribute("asset", port),
                 new XAttribute("portid", PortId1[portIdIndex++])
             ))
或者像这样:

int[] test = new int[3];
string[] PortName1 = new string[3];
string[] PortId1 = new string[3];
PortName1[0]="portname1";
PortName1[1] = "portname2";
PortName1[2] = "portname3";

PortId1[0] = "port1";
PortId1[1] = "port2";
PortId1[2] = "port3";
var fileName = @"C:\sample.xml";
var xdoc = XDocument.Load(fileName);

var server = 
    new XElement("server",
        new XAttribute("serverid", ServerId),
        new XAttribute("name", ServerName),
            test.Select(sample =>
                new XElement("port",
                    new XAttribute("asset", PortName),
                    new XAttribute("portid", PortId)
                ))
        );
var portIdIndex = 0; // declare variable somewhere before

PortName1.Select(port =>
             new XElement("port",
                 new XAttribute("asset", port),
                 new XAttribute("portid", PortId1[portIdIndex++])
             ))
或者,您可以使用匿名类型组合PortId和PortName:

PortName1.Select(port =>
             new XElement("port",
                 new XAttribute("asset", port),
                 new XAttribute("portid", PortId1[PortName1.IndexOf(port)])
             ))
从用户输入获取数据:

var portData = new [] { 
    new { PortName="Port 1", PortId = 1}, 
    new { PortName="Port 2", PortId = 2} 
};

portData.Select(port =>
     new XElement("port",
         new XAttribute("asset", port.PortName),
         new XAttribute("portid", port.PortId)
     ))

一点。

如果您使用的是.NET 4,您可以使用:

如果您不使用.NET4或更高版本,您可以在自己的代码中非常轻松地实现Zip—EricLippert已经做到了


作为补充说明,我鼓励您遵循.NET命名约定-例如,我会将PortId1和PortName1的名称更改为PortId和portNames。

:PortName和PortId是动态的,有时是2大小,有时是3大小,这取决于用户输入。您可以建议,在portdata下如何生成动态portname的&portId'sam getting error@Tuple我想Tuple是一个需要定义的类。它是众所周知的.Net类型,您使用的是.Net的哪个版本?它应该是可用的。嗯,好的,让我们使用数组,看看我的更新答案。@user1676709:您使用的Visual Studio版本实际上并不相关-您在项目中针对的是.NET的哪个版本?这将决定哪些类型可用。
var server = 
    new XElement("server",
        new XAttribute("serverid", ServerId),
        new XAttribute("name", ServerName),
            PortName1.Zip(PortId1, (name, id) =>
                new XElement("port",
                    new XAttribute("asset", name),
                    new XAttribute("portid", id)
                ))
        );