zoukankan      html  css  js  c++  java
  • C# xml读取操作

     以下xml:

    <Project>
      <ProjectMains>
        <ProjectMain Action="added">
          <ProjectID>AQZNSJC</ProjectID>
          <ProjectName>testproject</ProjectName>
          <BeginDate>2012/6/1 0:00:00</BeginDate>
          <EndDate>2020/6/1 0:00:00</EndDate>
          <LedgerID>1002</LedgerID>
          <InUsed>True</InUsed>
        </ProjectMain>
      </ProjectMains>  
    </Project>

    不带namespace的读取,直接用XPath方式即可:

                XmlDocument doc = new XmlDocument();
                var xml = TextBox1.Text.Trim();
                doc.LoadXml("<load>" + xml + "</load>");
                XmlNodeList ProList = doc.SelectNodes("//ProjectMains");
                foreach (XmlNode node in ProList)
                {
                    string ProjectID = node.SelectSingleNode("./ProjectMain/ProjectID").InnerText;
                }
    换成这样的<ProjectMains xmlns="http://someplace.org">,直接XPath方式就不行了,要AddNamespace后再带前缀去取:
           XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                nsmgr.AddNamespace("ab", "http://someplace.org");
                XmlNodeList ProList = doc.SelectNodes("//ab:ProjectMains",nsmgr);
                foreach (XmlNode node in ProList)
                {
                    string ProjectID = node.SelectSingleNode("//ab:ProjectMain/ab:ProjectID", nsmgr).InnerText;
                }

     RemoveNamespace后再去取似乎没什么效果:

    nsmgr.RemoveNamespace("ProjectMains", "http://someplace.org");

    最后碰到这个再多一层的情况,其实用AddNamespace去取就行了:

    <Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <ProjectMains xmlns="http://someplace.org">
        <ProjectMain Action="added">
          <ProjectID>AQZNSJC</ProjectID>
          <ProjectName>testproject</ProjectName>
          <BeginDate>2012/6/1 0:00:00</BeginDate>
          <EndDate>2020/6/1 0:00:00</EndDate>
          <BuildBeginDate>2015/11/2 16:41:00</BuildBeginDate>
          <BgnSaleDate>2015/11/2 16:41:00</BgnSaleDate>
          <BuildEndDate>2015/11/2 16:41:00</BuildEndDate>
          <EndSaleDate>2015/11/2 16:41:00</EndSaleDate>
          <Principaler>pp</Principaler>
          <ProjStatus />
          <OwnerCompanyID>F</OwnerCompanyID>
          <CityID>320684</CityID>
          <LedgerID>1002</LedgerID>
          <InUsed>True</InUsed>
        </ProjectMain>
      </ProjectMains>  
    </Project>
    <requestPubProfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <requestInfo xmlns="http://someplace.org">
        <requestID>000001</requestID>
        <correlationID />
        <version>1</version>
      </requestInfo>
    </requestPubProfile>
    <batchType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <batchInfo xmlns="http://someplace.org">
        <dataName>ProjectInfo</dataName>
        <dataCount>1</dataCount>
      </batchInfo>
    </batchType>

    或者实在不想处理这个前缀,那就直接取Tag吧:

    XmlNodeList projMainNodes = doc.GetElementsByTagName("ProjectMain");
    for (var i = 0; i < projMainNodes.Count; i++)
    {
        XmlElement groupElement = (XmlElement)projMainNodes.Item(i);// 转化成节点
        var bus = groupElement.GetElementsByTagName("ProjectID").Count > 0 ? groupElement.GetElementsByTagName("ProjectID")[0].InnerText 
                                             : string.Empty; }

    或者替换掉:

    xml = xml.Replace("xmlns=", "xmlns:xsi=");
    doc.LoadXml("<load>" + xml + "</load>");
    XmlNodeList ProList = doc.SelectNodes("//ProjectMains");
  • 相关阅读:
    Qt QTimer定时器相关
    C#Datetime和long之间转换
    C# 把图片资源转成字节数组写入到数据库
    Qt QProcess启动和关闭外部程序
    Qt绘图
    有哪些十分惊艳的书值得推荐3
    Stack Overflow 推荐编程书单
    《编写可读代码的艺术》的读书笔记 (脑图)
    [apache spark]洞见纽约车辆事故|bluemix|apache spark
    [lean scala]|How to create a SBT project with Intellij IDEA
  • 原文地址:https://www.cnblogs.com/pfs1314/p/4975548.html
Copyright © 2011-2022 走看看