C# 解析文本以创建XML
我从HttpWebRequest获得如下字符串:C# 解析文本以创建XML,c#,xml,regex,xml-serialization,text-parsing,C#,Xml,Regex,Xml Serialization,Text Parsing,我从HttpWebRequest获得如下字符串: Student■John■Smith ■Subject1■10■10 ■■Exam■A■B■C ■■Questions■B■A■C ■Subject2■223■227 Student■Ala■Cat ■Subject3■101■102 ■■Exam■A■B 我需要这样从中生成XML(将其反序列化为对象): 只是让你开始,不是一个完整的答案: char sep = '■'; //whatever. copy/paste with care.
Student■John■Smith
■Subject1■10■10
■■Exam■A■B■C
■■Questions■B■A■C
■Subject2■223■227
Student■Ala■Cat
■Subject3■101■102
■■Exam■A■B
我需要这样从中生成XML(将其反序列化为对象):
只是让你开始,不是一个完整的答案:
char sep = '■'; //whatever. copy/paste with care.
XElement lastStudent = null, lastDegrees = null;
foreach (var line in lineCollection)
{
string[] parts = line.Split(sep);
int leading = parts.TakeWhile(s => s == "").Count();
if (leading == 0) // Student
{
// todo: validate parts.length, part[0]
lastStudent = new XElement("Student",
new XAttribute("name", parts[1]),
new XAttribute("surname", parts[2]));
doc.Root.Add(lastStudent);
}
else if (leading == 1) // Subject
{
// todo: validate
lastDegrees = new XElement("degrees",
parts.Skip(2).Select(p => new XElement("degree", p)) );
lastStudent.Add(new XElement(parts[1], lastDegrees);
}
else ...
}
只是让你开始,不是一个完整的答案:
char sep = '■'; //whatever. copy/paste with care.
XElement lastStudent = null, lastDegrees = null;
foreach (var line in lineCollection)
{
string[] parts = line.Split(sep);
int leading = parts.TakeWhile(s => s == "").Count();
if (leading == 0) // Student
{
// todo: validate parts.length, part[0]
lastStudent = new XElement("Student",
new XAttribute("name", parts[1]),
new XAttribute("surname", parts[2]));
doc.Root.Add(lastStudent);
}
else if (leading == 1) // Subject
{
// todo: validate
lastDegrees = new XElement("degrees",
parts.Skip(2).Select(p => new XElement("degree", p)) );
lastStudent.Add(new XElement(parts[1], lastDegrees);
}
else ...
}
那些“块”字是什么意思?如果能知道那些黑色块实际上是什么,那就太好了。可能使用十六进制查看器或其他工具?还有,OP,您真的希望生成的XML格式不正确吗?这与HttpWebRequest没有任何关系,是吗?这是一个简单的文本解析问题。如果我错了,请纠正我,但您的问题与HttpWebRequest没有任何关系。这些“块”是什么角色的本意是什么?如果能知道这些黑块到底是什么就好了。可能使用十六进制查看器或其他工具?还有,OP,您真的希望生成的XML格式不正确吗?这与HttpWebRequest没有任何关系,是吗?这是一个简单的文本解析问题。如果我错了,请纠正我,但您的问题与HttpWebRequest没有任何关系。好的,它可以工作:D。但是如果我想将其更改为@HansKesting
建议的格式,该怎么办?好的,它可以工作:D。但是如果我想把它改成@HansKesting
所建议的那样呢?
char sep = '■'; //whatever. copy/paste with care.
XElement lastStudent = null, lastDegrees = null;
foreach (var line in lineCollection)
{
string[] parts = line.Split(sep);
int leading = parts.TakeWhile(s => s == "").Count();
if (leading == 0) // Student
{
// todo: validate parts.length, part[0]
lastStudent = new XElement("Student",
new XAttribute("name", parts[1]),
new XAttribute("surname", parts[2]));
doc.Root.Add(lastStudent);
}
else if (leading == 1) // Subject
{
// todo: validate
lastDegrees = new XElement("degrees",
parts.Skip(2).Select(p => new XElement("degree", p)) );
lastStudent.Add(new XElement(parts[1], lastDegrees);
}
else ...
}