求一正则表达式,或者截取函数,匹配<?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>)");这个方法 B/S不用服务器怎么实现(看清问题就给分) .net winform程序:如何在多个窗体公用一个ContextMenuStrip? 请教关于xml文件内“SYSTEM”引用文件路径在vs2005出现错误的问题 高手们帮个忙!!!!!! 如何让控间的大小和位置跟随窗体的大小变化而变化,就是相对位置相同 MediaPlayer播放音乐报错 一个代码问题 线程同步的问题? C# WinForm 中多人多机器时如何锁定同一个目录中的文件 Listbox控件在Surface平板中滑动的怪异现象 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>)");这个方法