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

 

posted @ 2015-11-19 10:49  peter cheng  阅读(2077)  评论(0编辑  收藏  举报