我上课了 PersonList
[XmlRoot("Persons")]
PersonList : List<Human>
Run Code Online (Sandbox Code Playgroud)
当我将其序列化为XML时,默认情况下会生成如下内容:
<Persons>
<Human>...</Human>
<Human>...</Human>
</Persons>
Run Code Online (Sandbox Code Playgroud)
我的问题是需要以改变元素做什么Human
,以Person
输出?所以输出将是:
<Persons>
<Person>...</Person>
<Person>...</Person>
</Persons>
Run Code Online (Sandbox Code Playgroud)
并且,如何将上述XML反序列化为PersonList
类对象?
Per Nick的建议,这是我的测试代码:
[XmlRoot("Persons")]
public class Persons : List<Human>
{
}
[XmlRoot("Person")]
public class Human
{
public Human()
{
}
public Human(string name)
{
Name = name;
}
[XmlElement("Name")]
public string Name { get; set; }
}
void TestXmlSerialize()
{
Persons personList = new Persons();
personList.Add(new Human("John"));
personList.Add(new Human("Peter"));
try
{
using (StringWriter writer = new StringWriter())
{ …
Run Code Online (Sandbox Code Playgroud) 我有一个string[]
,其中每个元素都以一些数字值结尾.
string[] partNumbers = new string[]
{
"ABC10", "ABC1","ABC2", "ABC11","ABC10", "AB1", "AB2", "Ab11"
};
Run Code Online (Sandbox Code Playgroud)
我试图按如下方式对上面的数组进行排序,LINQ
但我没有得到预期的结果.
var result = partNumbers.OrderBy(x => x);
Run Code Online (Sandbox Code Playgroud)
实际结果:
AB1
Ab11
AB2
ABC1
ABC10
ABC10
ABC11
ABC2
预期结果
AB1
AB2
AB11
..
我有这个结构:
struct Map
{
public int Size;
public Map ( int size )
{
this.Size = size;
}
public override string ToString ( )
{
return String.Format ( "Size: {0}", this.Size );
}
}
Run Code Online (Sandbox Code Playgroud)
使用数组时,它可以工作:
Map [ ] arr = new Map [ 4 ] {
new Map(10),
new Map(20),
new Map(30),
new Map(40)};
arr [ 2 ].Size = 0;
Run Code Online (Sandbox Code Playgroud)
但是当使用List时,它不会编译:
List<Map> list = new List<Map> ( ) {
new Map(10),
new Map(20),
new Map(30),
new Map(40)};
list [ 2 …
Run Code Online (Sandbox Code Playgroud) 我有一个问题,我只是想不通.我使用Eclipse创建自己的内容提供程序,但不断收到以下错误:
[..] ERROR/ActivityThread(1051):无法找到my.package.provider.countrycontentprovider的提供商信息
代码见:http://codepad.org/Rx00HjHd
主要部分:
public class CountryContentProvider extends ContentProvider {
public static final String PROVIDER =
"my.package.provider.countrycontentprovider";
public static final Uri CONTENT_URI =
Uri.parse("content://" + PROVIDER + "/country");
// ...
@Override
public boolean onCreate() { return true; }
// ...
}
// from my activity
ContentResolver resolver = getContentResolver();
Cursor c = resolver.query(CountryContentProvider.CONTENT_URI,
null, null, null, null);
// AndroidManifest.xml
<provider
android:name="my.package.provider.CountryContentProvider"
android:authorities="my.package.provider.countrycontentprovider" />
Run Code Online (Sandbox Code Playgroud)
我已将提供程序添加到清单中,并从onCreate
函数返回true .我CountryContentProvider.CONTENT_URI
在我的活动中使用来自我的提供者的内容,但我只是不断收到该错误消息.我删除并添加了三次代码(在日食融化的情况下)无济于事.
我肯定错过了什么.有人能指出我正确的方向吗?
我有一个System.Version
属性的类,看起来像这样:
当我序列化类时,版本始终为空:
<Version />
Run Code Online (Sandbox Code Playgroud)
Client类看起来像:
[Serializable]
public class Client
{
public string Description;
public string Directory;
public DateTime ReleaseDate;
public Version Version;
}
Run Code Online (Sandbox Code Playgroud) 我不知道如何在app.config中编写一条包含两行或更多行的消息.我在配置文件中的常用代码是:
add key="msg_test" value="This is test message."
Run Code Online (Sandbox Code Playgroud)
我用简单的代码阅读它:
ConfigurationSettings.AppSettings[msg_test];
Run Code Online (Sandbox Code Playgroud)
我想写点类似的东西
add key="msg_test"
value="This is test message \n are you sure you want to continue?"
Run Code Online (Sandbox Code Playgroud) 我刚接受采访.我被问到什么是"前瞻性声明".然后我被问到他们是否是与前瞻性声明有关的危险.
我无法回答第二个问题.在网上搜索没有显示任何有趣的结果.
那么,有人知道与使用前向声明有关的任何危险吗?
我有以下代码:
float f = 0.3f;
double d1 = System.Convert.ToDouble(f);
double d2 = System.Convert.ToDouble(f.ToString());
Run Code Online (Sandbox Code Playgroud)
结果相当于:
d1 = 0.30000001192092896;
d2 = 0.3;
Run Code Online (Sandbox Code Playgroud)
我很想知道为什么会这样?
.cpp
文件而不是.h
文件?翻译单位是否受到影响? .h
文件和.cpp
文件中都需要它,我应该将它包含在.h
文件中吗?这有关系吗? stdafx.h
)中,例如std和第三方库,这是一个好习惯吗?我自己的文件怎么样,我应该在stdafx.h
创建它们的过程中将它们包含在文件中? // myClass.h
#include <string>
// ^-------- should I include it here? --------
class myClass{
myClass();
~myClass();
int calculation()
};
// myClass.cpp
#include "myClass.h"
#include <string>
// ^-------- or maybe here? --------
[..]
int myClass::calculation(){
std::string someString = "Hello World";
return someString.length();
}
// stdafx.h
#include <string.h>
// ^--------- or perhaps here, and then include stdafx.h everywhere? -------
Run Code Online (Sandbox Code Playgroud)