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;
  • 相关阅读:
    2018牛客网暑期ACM多校训练营(第九场)A -Circulant Matrix(FWT)
    ZOJ
    BZOJ 4318 OSU!(概率DP)
    POJ
    POJ
    Linux安装及管理程序
    Linux目录及文件管理
    linux账号管理操作
    linux系统命令总结
    linux目录及文件管理操作
  • 原文地址:https://www.cnblogs.com/CCJVL/p/14084839.html
Copyright © 2011-2022 走看看