public void WriteXml(string strName,string strValue)
{
string[,] strNode = new string[,]{{"name","tradeItem" ,"tradeInfomation" ,"tradeing"} , {"subName","tradeItemDescriptionInformation" ,"tradeItemUnitDescriptor" ,"unit"} ,{"pubDate","tradeItemIdentification" ,"childTradeItem" ,"tradeItemIdentification"}};
for(int i=0 ;i < strNode.GetLength(0);i++)
{
if(strNode[i,0].ToString() == strName)
{
tid.strNode[i,1].ToString().strNode[i,2].ToString().strNode[i,3].ToString() = strValue;
}
}
}
如上面strNode是这样的关系,strNode[i,0]是字段名称,strNode[i,1]是对应的第一级类名,strNode[i,2]是对应第二级类名,strNode[i,3],是第二级类下面的属性名。
tid是名称空间.
我想问的是:在C#中采用什么技术,可以将方法写成:tid.变量类名.变量类名.变量属性名 = 具体的值.
而不用写一个属性就得写一个方法.
{
string[,] strNode = new string[,]{{"name","tradeItem" ,"tradeInfomation" ,"tradeing"} , {"subName","tradeItemDescriptionInformation" ,"tradeItemUnitDescriptor" ,"unit"} ,{"pubDate","tradeItemIdentification" ,"childTradeItem" ,"tradeItemIdentification"}};
for(int i=0 ;i < strNode.GetLength(0);i++)
{
if(strNode[i,0].ToString() == strName)
{
tid.strNode[i,1].ToString().strNode[i,2].ToString().strNode[i,3].ToString() = strValue;
}
}
}
如上面strNode是这样的关系,strNode[i,0]是字段名称,strNode[i,1]是对应的第一级类名,strNode[i,2]是对应第二级类名,strNode[i,3],是第二级类下面的属性名。
tid是名称空间.
我想问的是:在C#中采用什么技术,可以将方法写成:tid.变量类名.变量类名.变量属性名 = 具体的值.
而不用写一个属性就得写一个方法.
而不用写一个属性就得写一个方法.应该是不行的,毕竟“.”不能用作声明变量或属性
你的意思是:
tid.strName1.strName2.strName3 = strValue;这样吗?
strName1-3都是参数。
public class ListTest
{
private string[,,] strData;
public ListTest( int nFirst, int nSecond, int nThird )
{
strData = new string[nFirst, nSecond, nThird];
} public string this[string Name]
{
get
{
if( Name == null ) return null;
string[] strItems = Name.Split( '.' );
if( strItems == null || strItems.Length != 3 ) return null;
int i = -1, j = -1, k = -1;
try
{
i = Convert.ToInt32( strItems[0], 10 );
j = Convert.ToInt32( strItems[1], 10 );
k = Convert.ToInt32( strItems[2], 10 );
return strData[i,j,k];
}
catch{
return null;
} }
set
{
if( Name == null ) throw new Exception( "Invalid index name!" );
string[] strItems = Name.Split( '.' );
if( strItems == null || strItems.Length != 3 )
throw new Exception( "Invalid index name!" );
int i = -1, j = -1, k = -1;
try
{
i = Convert.ToInt32( strItems[0], 10 );
j = Convert.ToInt32( strItems[1], 10 );
k = Convert.ToInt32( strItems[2], 10 );
strData[i,j,k] = value;
}
catch
{
throw new Exception( "Invalid index name!" );
} }
}
}------Calling--------
ListTest myTest = new ListTest( 2,2,2 );
myTest["1.1.1"] = "Test";
MessageBox.Show( myTest["1.1.1"] );
一般的做法是写一个节点的结构,也就需要一个方法,而这样我就得写200多个方法。
现在比如说:
tid.tradeItem.tradeItemIdentification.additionalTradeItemIdentification = "100036";
就会生成这样的结构:
<tradeItem>
<tradeItemIdentification>
<gtin>100036</gtin>
</tradeItemIdentification>
</tradeItem>
而这个语句:
tid.tradeItem.tradeItemInformation.classificationCategoryCode.classificationCategoryCode = strCode;
生成这样的结构:
<tradeItem>
<tradeItemInformation>
<classificationCategoryCode>
<classificationCategoryCode>中国北京</classificationCategoryCode>
</classificationCategoryCode>
</tradeItemInformation>
</tradeItem>
上面所涉及的tradeItem、tradeItemInformation等是已经设计好的类库。
我现在就是直接调用这些类来写xml文件结构。
而我想要的这个语句:tid.strNode[i,1].ToString().strNode[i,2].ToString().strNode[i,3].ToString() = strValue;
也是为了执行上面那样类似的语句。
还请你多多执教!
要是象我的想的这个方法,应该怎么实现啊!
因为xml结构不固定的。
就是怕用反射啊,因为担心性能的问题啊
写200多个属性,执行200次这个方法,怕啊!
所以想寻找其他的途径。
to zhgroup()
我同意你的动态创建类的实例,要用反射的观点
同时对你的那个配置文件的方法很感兴趣,
能不能说的在具体一点了!