C# xml讀取操做

 如下xml:node

<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方式便可:ui

            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後再去取彷佛沒什麼效果:spa

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

 

最後碰到這個再多一層的狀況,其實用AddNamespace去取就好了:code

<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吧:xml

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; }

或者替換掉:blog

xml = xml.Replace("xmlns=", "xmlns:xsi=");
doc.LoadXml("<load>" + xml + "</load>");
XmlNodeList ProList = doc.SelectNodes("//ProjectMains");
相關文章
相關標籤/搜索