zoukankan      html  css  js  c++  java
  • Linq 读取 XML 注意事项

    读取XML的时候首先要注意BOM问题:

    以下为解决方案:

    string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
    if (xml.StartsWith(_byteOrderMarkUtf8))
    {
        xml = xml.Remove(0, _byteOrderMarkUtf8.Length);
    }

    另外需要注意,获取node value的时候需要加上namespace:

    参考:Linq to XML 之XElement的Descendants方法的新发现 - 天外飞雨 - 博客园 (cnblogs.com)

        XElement docX = XElement.Load(xmlName);
        
    //    var parentNode = "Folder";
        var childNode = docX.Name.Namespace + "Name";
    //    var childNodeValue = "10.9.1.1.1 Trigger Driver SW open";
    //    var tagetNodeValue = "ECU shall receive the trigger "Driver_open_switch"";
        var childNodeValue = groutName;
        var tagetNodeValue = itemName;
        var targetNode = docX.Name.Namespace + "TraceItem";
        var targetAttrib = docX.Name.Namespace + "UniqueID";
            
        XElement eleName = docX.Descendants(childNode).Where(itm => itm.Value == childNodeValue).First();//Find the element of <Name> which the value is childNodeValue        
        XElement xe = eleName.Parent;//Get the parent node    
        XElement eleTarget = xe.Descendants(childNode).Where(itm => itm.Value == tagetNodeValue).First();//Find the element of <Name> which the value is tagetNodeValue
        XElement xeP = eleTarget.Parent;//Get the parent node    
        XElement xeTarg = xeP.Descendants(targetAttrib).First();
        
        currPath1.Value = eleName.ToString();
        currPath2.Value = xe.ToString();
        currPath3.Value = xeTarg.ToString();
        
        id = int.Parse(xeTarg.Value.Replace("4999_2-",""));
        TmpReqID.Value=id;
  • 相关阅读:
    react-echarts之折线图的显示
    Log4j2
    测试驱动开发Junit4
    JavaWeb基础: Cookie
    JavaWeb前端:Bootstrap基础
    JavaWeb前端:CSS
    JavaWeb前端:JQuery
    Android基础:Activity
    JavaWeb前端: JavaScript 简介
    JavaWeb前端:HTML5 简介
  • 原文地址:https://www.cnblogs.com/CCJVL/p/14084839.html
Copyright © 2011-2022 走看看