ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C# xml读取操作

2019-07-13 20:00:25  阅读:212  来源: 互联网

标签:xml 00 读取 C# ProjectID nsmgr ProList doc


原文链接:http://www.cnblogs.com/pfs1314/p/4975548.html

 以下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");

 

转载于:https://www.cnblogs.com/pfs1314/p/4975548.html

标签:xml,00,读取,C#,ProjectID,nsmgr,ProList,doc
来源: https://blog.csdn.net/weixin_30888707/article/details/95782316

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有