【发布时间】:2014-09-10 20:10:52
【问题描述】:
我正在尝试在 xml 文件中写入一些我创建的配置文件,这些配置文件非常好,
输入字符串是 ProfilesList(0) = "45 65 67" ProfilesList(1) = "个人资料名称";
public void CreateGroupXML(String GroupNameWithPath, List<String> ProfilesList)
{
ProfilesGroup.ProfilesList = ProfilesList;
XmlWriterSettings ws = new XmlWriterSettings();
ws.NewLineHandling = NewLineHandling.Entitize;
for (int i = 0; i < ProfilesList.Count; i++)
{
ProfilesList[i] += Environment.NewLine;
}
XmlSerializer serializer = new XmlSerializer(typeof(ProfilesGroup));
using (XmlWriter wr = XmlWriter.Create(GroupNameWithPath, ws))
{
serializer.Serialize(wr, ProfilesGroup);
}
}
}
在 xml 文件中,配置文件是这样写的:
ProfilesList="45 65 67
 profilename

到目前为止一切顺利,当我尝试从 xml 文件中读取时会出现问题 它将第一个配置文件名称分成 3 这里是代码
public List<string> getProfilesOfGroup(string groupNameFullPath)
{
Stream stream = null;
try
{
stream = File.OpenRead(groupNameFullPath);
XmlSerializer serializer = new XmlSerializer(typeof(ProfilesGroup));
_ProfilesGroup = (ProfilesGroup)serializer.Deserialize(stream);
stream.Close();
return _ProfilesGroup.ProfilesList;
}
catch (Exception Ex)
{
log.ErrorFormat("Exception in getProfilesOfGroup: {0}", Ex.Message);
if (stream != null)
{
stream.Close();
}
return null;
}
}
the output (lets call the string ProfileList) contains :
ProfileList(0) = 45
ProfileList(1) = 65
ProfileList(2) = 67
ProfileList(3) = profilename
and i expecting the string to contain
ProfileList(0) = 45 65 67
ProfileList(1) = profilename
在此处编辑完整的 xml:
?xml version="1.0" encoding="utf-8"?ProfilesGroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ProfilesList="45 65 67 个人资料名称 "
和班级:
[XmlRootAttribute("VProfilesGroup", IsNullable = false, DataType = "", Namespace = "")]
public class ProfilesGroup
{
[XmlAttribute("ProfilesList")]
public List<String> ProfilesList = new List<string>();
}
【问题讨论】:
-
这不是正常的行为,因为您要返回一个 List 如果您要返回一个字符串 [] 会发生什么情况,那么您是否只能获得 2 个项目而不是 4 个项目,您是否可以不使用 Join() 方法
-
是的,但我希望得到一个长度为 2 而不是 4 的列表
-
dbc 我编辑了请看@dbc
-
向我们展示一些 XML 怎么样?
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
标签: c# xml xml-parsing