我想在XML編寫的文件,我創建到目前爲止好一些資料,讀取線和保持空間
輸入字符串爲ProfilesList(0)=「45 65 67」 ProfilesList(1 )=「profilename」;
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 PROFILENAME 」
和類:
[XmlRootAttribute("VProfilesGroup", IsNullable = false, DataType = "", Namespace = "")]
public class ProfilesGroup
{
[XmlAttribute("ProfilesList")]
public List<String> ProfilesList = new List<string>();
}
不正常的行爲,因爲你是返回一個列表,如果你要返回一個字符串[]你會那麼只能獲得2項對4你能不能用一個加入會發生什麼( )方法 – MethodMan 2014-09-10 20:16:54
是的,但我不贊同獲得列表2的lenegth而不是4 – Zlex 2014-09-10 20:28:31
dbc我編輯請看看@dbc – Zlex 2014-09-10 20:38:07