关于c#:读取行并保留xml文件中的空格

read line and keep spaces from xml file

我正在尝试用XML文件编写一些我迄今为止创建的配置文件,

输入字符串是profileslist(0)="45 65 67"profileslist(1)="配置文件名";

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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文件中,这样编写的配置文件:

1
ProfilesList="45 65 67&#xA; profilename&#xA;

到目前为止还不错,当我尝试读取XML文件时,问题就出现了。它将第一个配置文件名分为3个这里是密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    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 "

课程:

1
2
3
4
5
6
[XmlRootAttribute("VProfilesGroup", IsNullable = false, DataType ="", Namespace ="")]
public class ProfilesGroup
{
    [XmlAttribute("ProfilesList")]
    public List<String> ProfilesList = new List<string>();
}


为什么不直接删除[XmlAttribute("ProfilesList")]属性?您的数据将成功序列化和反序列化。XML将如下所示:

1
2
3
4
5
6
<VProfilesGroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <ProfilesList>
        <string>45 65 67</string>
        <string>profilename</string>
    </ProfilesList>
</VProfilesGroup>

在这种格式中,字符串列表明确定义为有两个条目。这是使用XmlSerializer对字符串数组进行序列化和反序列化的标准方法。或者,是否有一些外部约束使您将列表声明为属性?

更新

如果必须将ProfilesList序列化为属性而不是元素数组,则可以像这样手动构造和解构字符串:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[XmlRootAttribute("VProfilesGroup", IsNullable = false, DataType ="", Namespace ="")]
public class ProfilesGroup
{
    static readonly char Delimiter = '
'
;

    [XmlIgnore]
    public List<String> ProfilesList { get; set; } // Enhance the setter to throw an exception if any string contains the delimiter.

    [XmlAttribute("ProfilesList")]
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public string ProfilesListText
    {
        get
        {
            return string.Join(Delimiter.ToString(), ProfilesList.ToArray());
        }
        set
        {
            ProfilesList = new List<string>(value.Split(Delimiter));
        }
    }

    public static string CreateGroupXML(List<String> ProfilesList)
    {
        var group = new ProfilesGroup();
        group.ProfilesList = ProfilesList;
        return XmlSerializationHelper.GetXml(group);
    }

    public static List<string> GetProfilesOfGroup(string xml)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(ProfilesGroup));
        var group = (ProfilesGroup)serializer.Deserialize(new StringReader(xml));
        return group == null ? null : group.ProfilesList;
    }

    public static void Test()
    {
        List<string> list = new List<string>(new string[] {"45 65 67","profilename" });
        var xml = CreateGroupXML(list);
        var newList = GetProfilesOfGroup(xml);
        bool same = list.SequenceEqual(newList);
        Debug.Assert(same); // No assert.
    }
}

生成的XML如下所示:

1
2
3
<?xml version="1.0" encoding="utf-16"?>

<VProfilesGroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ProfilesList="45 65 67&#xA;profilename" />

在本例中,我通过将代码序列化和反序列化为字符串而不是文件来测试代码。然后助手:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static class XmlSerializationHelper
{
    public static string GetXml<T>(T obj, XmlSerializer serializer) where T : class
    {
        using (var textWriter = new StringWriter())
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;        // For cosmetic purposes.
            settings.IndentChars ="   "; // The indentation used in the test string.
            using (var xmlWriter = XmlWriter.Create(textWriter, settings))
            {
                serializer.Serialize(xmlWriter, obj);
            }
            return textWriter.ToString();
        }
    }

    public static string GetXml<T>(T obj) where T : class
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        return GetXml(obj, serializer);
    }
}