C#如何在新类中存储字符串列表? [英] C# how to store a list of strings in a new class?
问题描述
大家好!
我有一个XML,我从那里读取和存储数据。
喜欢:
< root>
< main>
< 1>样本< / 1>
< 2>样本< / 2>
< context>样本< / context>
< context>样本< / context>
< context>样本< / context>
< context>样本< / context>
< / main>
< main>
< 1>样本< / 1>
< 2>样本< / 2>
< context>样本< / context>
< context>样本< / context>
< context>样本< / context>
< / main>
< main>
< 1>样本< / 1>
< 2>样本< / 2>
< / main>
< / root>
出于某种原因使用下面的代码,我无法存储每个类的上下文列表。
我的尝试:
我正在保存所有像这样的新类的主要:
ht.Add(new HandledTUs {SOURCE = 1,TARGET = 2,context = context,});
课程如下:
公共课程TU
{
private List< string> _CONTEXT = new List< string>();
public string 1 {get;组; }
public string 2 {get;组; }
public List< string>语境{get {return _CONTEXT; } set {_CONTEXT = value; }
}
添加理查德所说的,1
和2
不是值变量或属性名称,不能以数字字符开头。
所以你的C#代码很糟糕 - 它不会编译 - 同样适用于XML字段名称,它也必须以下划线字母开头。
所以你的代码很糟糕,yoru数据很糟糕 - 我对你使用的任何代码都不起作用并不感到惊讶!
如果我更正你的xml:
< pre lang =HTML> <? xml version = 1.0 encoding = utf-8 ?>
< root >
< main >
< A1 > 示例< / A1 >
< A2 > 示例< / A2 >
< context > 示例< / context >
< context & gt; 示例< / context > ;
< context > 示例< / context > ;
< context > 示例< / context > ;
< / main >
< main >
< A1 > 示例< / A1 >
< A2 > 示例< / A2 >
< context > 示例< / context >
< context > 示例< / context >
< context > 示例< < span class =code-leadattribute> / context >
< / main >
< main >
< A1 > 示例< / A1 >
< A2 > 示例< / A2 >
< / main >
< / root >并使用VS生成类:
/// < 备注/ >
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true )]
[ System.Xml.Serialization.XmlRootAttribute(Namespace = ,IsNullable = false )]
public partial class root
{
私人 rootMain [] mainField;
/// < 备注/ >
[System.Xml.Serialization.XmlElementAttribute( main)]
public rootMain [] main
{
get
{
return 此 .mainField;
}
set
{
this .mainField = 值;
}
}
}
/// < 备注/ >
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true )]
public partial class rootMain
{
private string a1Field;
private string a2Field;
private string [] contextField;
/// < 备注/ >
public string A1
{
get
{
return this .a1Field;
}
set
{
this .a1Field = 值;
}
}
/// < 备注/ >
public string A2
{
get
{
return this .a2Field;
}
set
{
this .a2Field = 值;
}
}
/// < 备注/ >
[System.Xml.Serialization.XmlElementAttribute( context)]
public string [] context
{
get
{
return this .contextField;
}
set
{
this .contextField = 值;
}
}
}然后rootMain类看起来像你的TUs类。
(VS可以从XML生成XML类data - 创建一个新的空类文件,然后从菜单栏中选择Edit ... Paste Special ... Paste XML as classes,它将根据剪贴板数据生成类。)
Hi all!
I have an XML and I read and store data from there.
like:
<root> <main> <1>sample</1> <2>sample</2> <context>sample</context> <context>sample</context> <context>sample</context> <context>sample</context> </main> <main> <1>sample</1> <2>sample</2> <context>sample</context> <context>sample</context> <context>sample</context> </main> <main> <1>sample</1> <2>sample</2> </main> </root>
For some reason with the code below I can't store a list of context per class.
What I have tried:
I'm saving all the "main" to a new class like this:
ht.Add(new HandledTUs { SOURCE = 1, TARGET = 2, context = context,});
Class looks like this:
public class TUs { private List<string> _CONTEXT = new List<string>(); public string 1{ get; set; } public string 2{ get; set; } public List<string> CONTEXT { get { return _CONTEXT; } set { _CONTEXT = value; } } }
To add to what Richard says,1
and2
are not value variable or property names, which cannot start with a numeric character.
So your C# code is bad - it won't compile - and the same applies to XML field names which must also start with a letter of underscore.
So your code is bad, yoru data is bad - I'm not surprised that whatever code you are using won't work!
If I correct your xml:
<?xml version="1.0" encoding="utf-8" ?> <root> <main> <A1>sample</A1> <A2>sample</A2> <context>sample</context> <context>sample</context> <context>sample</context> <context>sample</context> </main> <main> <A1>sample</A1> <A2>sample</A2> <context>sample</context> <context>sample</context> <context>sample</context> </main> <main> <A1>sample</A1> <A2>sample</A2> </main> </root>And use VS to generate the classes:
/// <remarks/> [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] public partial class root { private rootMain[] mainField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute("main")] public rootMain[] main { get { return this.mainField; } set { this.mainField = value; } } } /// <remarks/> [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public partial class rootMain { private string a1Field; private string a2Field; private string[] contextField; /// <remarks/> public string A1 { get { return this.a1Field; } set { this.a1Field = value; } } /// <remarks/> public string A2 { get { return this.a2Field; } set { this.a2Field = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute("context")] public string[] context { get { return this.contextField; } set { this.contextField = value; } } }Then the rootMain class looks like your TUs class.
(VS can generate XML classes for you from XML data - create a new empty class file, and select "Edit...Paste Special...Paste XML as classes" from the menu bar and it will generate classes based on the clipboard data.)
这篇关于C#如何在新类中存储字符串列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!