求一正则表达式,或者截取函数,匹配<?xml.*</EMC>中内容,文档中可能有多个块,还有不正常的格式 这个东西你为什么不用xmldocument去操作xml文件呢,那样读取多方便 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是一个数据流,要一个个处理,得到一个结构体之后序列到类里面去,如果得不到一个完整的结构体序列会出错,即使使用xmldocument去操作,也要把这些数据流分割成一个个的完整格式,要把这个数据舍弃掉。 是一个数据流,要一个个处理,得到一个结构体之后序列到类里面去,如果得不到一个完整的结构体序列会出错,即使使用xmldocument去操作,也要把这些数据流分割成一个个的完整格式,要把这个数据舍弃掉。 private void btn_Click(object sender, RoutedEventArgs e) { string s = @"<?xml version=""1.0""?><EMC xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <Command>xxxx</Command> <UnitName>单位</UnitName></ZXEMC> <UnitCode>单位</UnitCode></EMC><?xml version=""1.0""?><EMC xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <Command>xxxx</Command> <UnitName>单位</</ZXEMC><?xml version=""1.0""?><EMC xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <Command>xxxx</Command> <UnitName>单位</UnitName> <UnitCode>单位</UnitCode></EMC></ZXEMC>"; MatchCollection matches = Regex.Matches(s, @"(?is)(<\?xml[\s\S]*?</zxemc>)"); foreach (Match m in matches) { Console.WriteLine(m.Groups[1].Value); } } 你是要截取<?xml.*</EMC>里面的内容还是<?xml.*</ZXEMC>里面的内容MatchCollection matches = Regex.Matches(s, @"(?is)(<\?xml[\s\S]*?</zxemc>)");//或者MatchCollection matches = Regex.Matches(s, @"(?is)(<\?xml[\s\S]*?</emc>)"); 你这个结果不符合要求:MatchCollection matches = Regex.Matches(s, @"(?is)(<\?xml[\s\S]*?</zxemc>)");使用这个我试过了结果如下:2个对象对象1<?xml version="1.0"?><EMC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Command>xxxx</Command> <UnitName>单位</UnitName> <UnitCode>单位</UnitCode></EMC>对象2<?xml version="1.0"?><EMC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Command>xxxx</Command> [color=#FF6600]<UnitName>单位</[/color]<?xml version="1.0"?><EMC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Command>xxxx</Command> <UnitName>单位</UnitName> <UnitCode>单位</UnitCode></EMC>红色部分都还在,我用其它方法解决了 谢谢 你这个结果不符合要求:MatchCollection matches = Regex.Matches(s, @"(?is)(<\?xml[\s\S]*?</zxemc>)");使用这个我试过了结果如下:2个对象对象1<?xml version="1.0"?><EMC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Command>xxxx</Command> <UnitName>单位</UnitName> <UnitCode>单位</UnitCode></EMC>对象2<?xml version="1.0"?><EMC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Command>xxxx</Command> [color=#FF6600]<UnitName>单位</[/color]<?xml version="1.0"?><EMC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Command>xxxx</Command> <UnitName>单位</UnitName> <UnitCode>单位</UnitCode></EMC>红色部分都还在,我用其它方法解决了 谢谢MatchCollection matches = Regex.Matches(s, @"(?is)(<\?xml[\s\S]*?</emc>)");这个方法 C#学习中,望大家指导哈 C#程序在别人的机器上无法正确运行 C# 回调函数 线程问题 比较ASCII码大小 关于picturebox的问题,解决了马上结贴 using(connection.open()){}能代替 try{}catch{}finally{}吗? 程序实现的跑马灯卡顿 一道面试题,请帮忙解答 求 1000!的位数 有什么好办法呢 如何判断某一天是某个月中的第几周的第几天? C#读写二进制文件该如何下手? MQ Client 客户端的问题 convert转换失败不会抛异常吗
即使使用xmldocument去操作,也要把这些数据流分割成一个个的完整格式,要把这个数据舍弃掉。
是一个数据流,要一个个处理,得到一个结构体之后序列到类里面去,如果得不到一个完整的结构体序列会出错,
即使使用xmldocument去操作,也要把这些数据流分割成一个个的完整格式,要把这个数据舍弃掉。
private void btn_Click(object sender, RoutedEventArgs e)
{
string s = @"<?xml version=""1.0""?>
<EMC xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<Command>xxxx</Command>
<UnitName>单位</UnitName></ZXEMC>
<UnitCode>单位</UnitCode>
</EMC><?xml version=""1.0""?>
<EMC xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<Command>xxxx</Command>
<UnitName>单位</</ZXEMC><?xml version=""1.0""?>
<EMC xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<Command>xxxx</Command>
<UnitName>单位</UnitName>
<UnitCode>单位</UnitCode>
</EMC></ZXEMC>";
MatchCollection matches = Regex.Matches(s, @"(?is)(<\?xml[\s\S]*?</zxemc>)");
foreach (Match m in matches)
{
Console.WriteLine(m.Groups[1].Value);
}
}
//或者
MatchCollection matches = Regex.Matches(s, @"(?is)(<\?xml[\s\S]*?</emc>)");
我试过了结果如下:2个对象
对象1
<?xml version="1.0"?>
<EMC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Command>xxxx</Command>
<UnitName>单位</UnitName>
<UnitCode>单位</UnitCode>
</EMC>
对象2
<?xml version="1.0"?>
<EMC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Command>xxxx</Command>
[color=#FF6600]<UnitName>单位</
[/color]
<?xml version="1.0"?>
<EMC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Command>xxxx</Command>
<UnitName>单位</UnitName>
<UnitCode>单位</UnitCode>
</EMC>
红色部分都还在,我用其它方法解决了 谢谢
我试过了结果如下:2个对象
对象1
<?xml version="1.0"?>
<EMC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Command>xxxx</Command>
<UnitName>单位</UnitName>
<UnitCode>单位</UnitCode>
</EMC>
对象2
<?xml version="1.0"?>
<EMC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Command>xxxx</Command>
[color=#FF6600]<UnitName>单位</
[/color]
<?xml version="1.0"?>
<EMC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Command>xxxx</Command>
<UnitName>单位</UnitName>
<UnitCode>单位</UnitCode>
</EMC>
红色部分都还在,我用其它方法解决了 谢谢
MatchCollection matches = Regex.Matches(s, @"(?is)(<\?xml[\s\S]*?</emc>)");这个方法