首页 > 技术文章 > C#读取XML格式字符串

zyx321 2017-03-02 16:29 原文

 System.Text.StringBuilder res = new System.Text.StringBuilder();
            res.Append(" <?xml version='1.0' encoding='utf-8'?>");
            res.Append("<YG_HCCGFHXX>");
            res.Append("<XMLDATA>");
            res.Append("<HEAD>");
            res.Append("<JSSJ>接收时间</JSSJ>");
            res.Append("<ZTCLJG>消息主体处理结果</ZTCLJG>");
            res.Append("<CWXX>错误提示内容</CWXX>");
            res.Append("<BZXX>备注信息</BZXX>");
            res.Append("</HEAD>");
            res.Append("<MAIN>");
            res.Append("<DDBH>采购单号</DDBH>");
            res.Append("</MAIN>");
            res.Append("<DETAIL>");
            res.Append("<STRUCT>");
            res.Append("<DDMXBH>采购单明细编号</DDMXBH>");
            res.Append("<SXH>顺序号</SXH>");
            res.Append("<HCTBDM>耗材统编代码</HCTBDM>");
            res.Append("<HCXFDM>耗材细分代码</HCXFDM>");
            res.Append("<YYBDDM>医院本地代码</YYBDDM>");
            res.Append("<QYKC>企业库存</QYKC>");
            res.Append("<CLJG>处理结果</CLJG>");
            res.Append("<CLQKMS>处理情况描述</CLQKMS>");
            res.Append("</STRUCT>");
            res.Append("</DETAIL>");
            res.Append("</XMLDATA>");
            res.Append("</YG_HCCGFHXX>");


XElement root = XElement.Parse(@"" + res.ToString().Trim() + "");

            //采购单号
            string DDBH = root.Element("XMLDATA").Element("MAIN").Element("DDBH").Value;
            //与采购单明细号
            string DDMXBH = root.Element("XMLDATA").Element("DETAIL").Element("STRUCT").Element("DDMXBH").Value;

 

推荐阅读