首页 新闻 会员 周边 捐助

C# 读取XML 根级别上的数据无效。 第 1 行,位置 1。

0
悬赏园豆:5 [已解决问题] 解决于 2014-03-05 14:05

string blogValue = "0xEFBBBF3C3F786D6C2076657273696F6E3D22312E302220656E636F64696E673D225554462D38223F3E0D0A0D0A3C70726F63657373206E616D653D22E680BBE79B91E5A794E4BBBBE4B9A6E794B3E8AFB72220786D6C6E733D22687474703A2F2F6A62706D2E6F72672F342E342F6A70646C223E0D0A2020203C7374617274206E616D653D22737461727422203E0D0A2020202020203C7472616E736974696F6E206E616D653D2273746172742220746F3D22E794B3E8AFB7E4BABAE68F90E4BAA42220666F726D3D222F436F6D6D697373696F6E4170706C69636174696F6E2F4469726563746F725265636F7264526576696577222F3E0D0A2020203C2F73746172743E0D0A2020203C7461736B206E616D653D22E794B3E8AFB7E4BABAE68F90E4BAA4222061737369676E65653D22237B6F776E65727D22203E0D0A0920200D0A2020202020203C7472616E736974696F6E206E616D653D227375626D69742220746F3D22E7949FE4BAA7E983A8E997A8E8B49FE8B4A3E4BABAE5AEA1E6A0B82220666F726D3D222F436F6D6D697373696F6E4170706C69636174696F6E2F4469726563746F725265636F7264526576696577222F3E0D0A2020203C2F7461736B3E0D0A200D0A2020203C7461736B206E616D653D22E7949FE4BAA7E983A8E997A8E8B49FE8B4A3E4BABAE5AEA1E6A0B8222061737369676E65653D22237B61756469746F727D2220666F726D3D222F436F6D6D697373696F6E4170706C69636174696F6E2F4469726563746F725265636F726452657669657722203E0D0A2020202020203C7472616E736974696F6E206E616D653D226163636570742220746F3D22666F726B22202F3E0D0A2020202020203C7472616E736974696F6E206E616D653D2272656A6563742220746F3D22E98080E59B9EE794B3E8AFB7E4BABA22202F3E0D0A2020203C2F7461736B3E0D0A20203C666F726B206E616D653D22666F726B223E0D0A202020203C7472616E736974696F6E206E616D653D22312220746F3D22E7BB8FE890A5E983A8E5AEA1E689B922202F3E0D0A202020203C7472616E736974696F6E206E616D653D22322220746F3D22E68A80E69CAFE8B4A8E9878FE983A8E5AEA1E689B922202F3E0D0A20203C2F666F726B3E0D0A20203C7461736B206E616D653D22E7BB8FE890A5E983A8E5AEA1E689B922202061737369676E65653D22237B627573696E6573737D2220666F726D3D222F436F6D6D697373696F6E4170706C69636174696F6E2F4469726563746F725265636F726452657669657722203E0D0A0D0A202020203C7472616E736974696F6E206E616D653D226163636570742220746F3D226A6F696E22202F3E0D0A20202020203C7472616E736974696F6E206E616D653D2272656A6563742220746F3D22E98080E59B9EE794B3E8AFB7E4BABA22202F3E0D0A20203C2F7461736B3E0D0A20203C7461736B206E616D653D22E68A80E69CAFE8B4A8E9878FE983A8E5AEA1E689B922202061737369676E65653D22237B7175616C6974797D2220666F726D3D222F436F6D6D697373696F6E4170706C69636174696F6E2F4469726563746F725265636F726452657669657722203E0D0A0D0A202020203C7472616E736974696F6E206E616D653D226163636570742220746F3D226A6F696E22202F3E0D0A203C7472616E736974696F6E206E616D653D2272656A6563742220746F3D22E98080E59B9EE794B3E8AFB7E4BABA22202F3E0D0A20203C2F7461736B3E0D0A20203C6A6F696E206E616D653D226A6F696E223E0D0A202020203C7472616E736974696F6E206E616D653D226163636570742220746F3D226F6B22202F3E0D0A20203C2F6A6F696E3E0D0A202020203C7461736B206E616D653D226F6B223E0D0A2020202020203C7472616E736974696F6E206E616D653D226163636570742220746F3D22656E6422202F3E0D0A2020203C2F7461736B3E0D0A2020203C7461736B206E616D653D22E98080E59B9EE794B3E8AFB7E4BABA222061737369676E65653D22237B6F776E65727D2220666F726D3D222F436F6D6D697373696F6E4170706C69636174696F6E2F4469726563746F725265636F726452657669657722203E0D0A20202020200D0A2020202020203C7472616E736974696F6E206E616D653D22746F656E642220746F3D22656E6422202F3E0D0A2020203C2F7461736B3E0D0A2020203C656E64206E616D653D22656E6422202F3E0D0A2020200D0A3C2F70726F636573733E0D0A20";

            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(blogValue);
            MemoryStream ms = new MemoryStream(buffer);
            string xml = System.Text.Encoding.UTF8.GetString(buffer);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

 

 

 

anhuisunfei的主页 anhuisunfei | 初学一级 | 园豆:6
提问于:2013-11-18 10:45
< >
分享
最佳答案
0
        public void Test1()
        {
            //0、观察发现,这个字符串是:UTF8编码存储的字符串的内存表示
            //1、这个字符串有一个UTF8的BOM:"0xEFBBBF",首先要去掉它
            //2、其次,这个字符串实际上是将字节数组每个字节表示为两个16进制数字,并最终输出为一个字符串
            string blogValue = "0xEFBBBF3C3F786D6C2076657273696F6E3D22312E302220656E636F64696E673D225554462D38223F3E0D0A0D0A3C70726F63657373206E616D653D22E680BBE79B91E5A794E4BBBBE4B9A6E794B3E8AFB72220786D6C6E733D22687474703A2F2F6A62706D2E6F72672F342E342F6A70646C223E0D0A2020203C7374617274206E616D653D22737461727422203E0D0A2020202020203C7472616E736974696F6E206E616D653D2273746172742220746F3D22E794B3E8AFB7E4BABAE68F90E4BAA42220666F726D3D222F436F6D6D697373696F6E4170706C69636174696F6E2F4469726563746F725265636F7264526576696577222F3E0D0A2020203C2F73746172743E0D0A2020203C7461736B206E616D653D22E794B3E8AFB7E4BABAE68F90E4BAA4222061737369676E65653D22237B6F776E65727D22203E0D0A0920200D0A2020202020203C7472616E736974696F6E206E616D653D227375626D69742220746F3D22E7949FE4BAA7E983A8E997A8E8B49FE8B4A3E4BABAE5AEA1E6A0B82220666F726D3D222F436F6D6D697373696F6E4170706C69636174696F6E2F4469726563746F725265636F7264526576696577222F3E0D0A2020203C2F7461736B3E0D0A200D0A2020203C7461736B206E616D653D22E7949FE4BAA7E983A8E997A8E8B49FE8B4A3E4BABAE5AEA1E6A0B8222061737369676E65653D22237B61756469746F727D2220666F726D3D222F436F6D6D697373696F6E4170706C69636174696F6E2F4469726563746F725265636F726452657669657722203E0D0A2020202020203C7472616E736974696F6E206E616D653D226163636570742220746F3D22666F726B22202F3E0D0A2020202020203C7472616E736974696F6E206E616D653D2272656A6563742220746F3D22E98080E59B9EE794B3E8AFB7E4BABA22202F3E0D0A2020203C2F7461736B3E0D0A20203C666F726B206E616D653D22666F726B223E0D0A202020203C7472616E736974696F6E206E616D653D22312220746F3D22E7BB8FE890A5E983A8E5AEA1E689B922202F3E0D0A202020203C7472616E736974696F6E206E616D653D22322220746F3D22E68A80E69CAFE8B4A8E9878FE983A8E5AEA1E689B922202F3E0D0A20203C2F666F726B3E0D0A20203C7461736B206E616D653D22E7BB8FE890A5E983A8E5AEA1E689B922202061737369676E65653D22237B627573696E6573737D2220666F726D3D222F436F6D6D697373696F6E4170706C69636174696F6E2F4469726563746F725265636F726452657669657722203E0D0A0D0A202020203C7472616E736974696F6E206E616D653D226163636570742220746F3D226A6F696E22202F3E0D0A20202020203C7472616E736974696F6E206E616D653D2272656A6563742220746F3D22E98080E59B9EE794B3E8AFB7E4BABA22202F3E0D0A20203C2F7461736B3E0D0A20203C7461736B206E616D653D22E68A80E69CAFE8B4A8E9878FE983A8E5AEA1E689B922202061737369676E65653D22237B7175616C6974797D2220666F726D3D222F436F6D6D697373696F6E4170706C69636174696F6E2F4469726563746F725265636F726452657669657722203E0D0A0D0A202020203C7472616E736974696F6E206E616D653D226163636570742220746F3D226A6F696E22202F3E0D0A203C7472616E736974696F6E206E616D653D2272656A6563742220746F3D22E98080E59B9EE794B3E8AFB7E4BABA22202F3E0D0A20203C2F7461736B3E0D0A20203C6A6F696E206E616D653D226A6F696E223E0D0A202020203C7472616E736974696F6E206E616D653D226163636570742220746F3D226F6B22202F3E0D0A20203C2F6A6F696E3E0D0A202020203C7461736B206E616D653D226F6B223E0D0A2020202020203C7472616E736974696F6E206E616D653D226163636570742220746F3D22656E6422202F3E0D0A2020203C2F7461736B3E0D0A2020203C7461736B206E616D653D22E98080E59B9EE794B3E8AFB7E4BABA222061737369676E65653D22237B6F776E65727D2220666F726D3D222F436F6D6D697373696F6E4170706C69636174696F6E2F4469726563746F725265636F726452657669657722203E0D0A20202020200D0A2020202020203C7472616E736974696F6E206E616D653D22746F656E642220746F3D22656E6422202F3E0D0A2020203C2F7461736B3E0D0A2020203C656E64206E616D653D22656E6422202F3E0D0A2020200D0A3C2F70726F636573733E0D0A20";
            if (blogValue.StartsWith("0xEFBBBF"))
            {
                blogValue = blogValue.TrimStart("0xEFBBBF".ToArray());
            }
            var bytes = new List<byte>();
            for (int i = 0; i < blogValue.Length; i += 2)
            {
                //2个字符转1个byte
                bytes.Add(byte.Parse("" + blogValue[i] + blogValue[i + 1], NumberStyles.HexNumber));
            }
            
            var buffer = bytes.ToArray();
            MemoryStream ms = new MemoryStream(buffer);
            string xml = System.Text.Encoding.UTF8.GetString(buffer);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
        }

最终解析出来,这段字符串是:

<?xml version="1.0" encoding="UTF-8"?>

<process name="总监委任书申请" xmlns="http://jbpm.org/4.4/jpdl">
   <start name="start" >
      <transition name="start" to="申请人提交" form="/CommissionApplication/DirectorRecordReview"/>
   </start>
   <task name="申请人提交" assignee="#{owner}" >
      
      <transition name="submit" to="生产部门负责人审核" form="/CommissionApplication/DirectorRecordReview"/>
   </task>
 
   <task name="生产部门负责人审核" assignee="#{auditor}" form="/CommissionApplication/DirectorRecordReview" >
      <transition name="accept" to="fork" />
      <transition name="reject" to="退回申请人" />
   </task>
  <fork name="fork">
    <transition name="1" to="经营部审批" />
    <transition name="2" to="技术质量部审批" />
  </fork>
  <task name="经营部审批"  assignee="#{business}" form="/CommissionApplication/DirectorRecordReview" >

    <transition name="accept" to="join" />
     <transition name="reject" to="退回申请人" />
  </task>
  <task name="技术质量部审批"  assignee="#{quality}" form="/CommissionApplication/DirectorRecordReview" >

    <transition name="accept" to="join" />
 <transition name="reject" to="退回申请人" />
  </task>
  <join name="join">
    <transition name="accept" to="ok" />
  </join>
    <task name="ok">
      <transition name="accept" to="end" />
   </task>
   <task name="退回申请人" assignee="#{owner}" form="/CommissionApplication/DirectorRecordReview" >
     
      <transition name="toend" to="end" />
   </task>
   <end name="end" />
   
</process>
 

 

 另外,题主的这个字符串,在数据库中就是存为字节数组的,所以取出来应该是字节数组,直接用xmlDoc.Load(Stream inStream)就可以读出来了。

收获园豆:5
Shine Ss | 菜鸟二级 |园豆:214 | 2013-11-18 16:15
其他回答(3)
0

建议用Linq试下,XElement.Load(fileName);

平常心队长 | 园豆:1113 (小虾三级) | 2013-11-18 10:50
0

这转出来会是一个XML格式的吗?

Zery | 园豆:6151 (大侠五级) | 2013-11-18 10:51

是的 这是JBPM存储到数据库的

支持(0) 反对(0) anhuisunfei | 园豆:6 (初学一级) | 2013-11-18 10:53
0

图片存到数据库也不应该是xml吧,应该是二进制呢。你确认下这个确实能转成xml吗?

幻天芒 | 园豆:37207 (高人七级) | 2013-11-18 12:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册