簡體   English   中英

從xml元素C#中選擇節點

[英]Selecting nodes from xml element c#

我想做點什么,但我不知道怎么做。 Myxml看起來像這樣

<Person xmlns="http://askmk/ask/ReportTypes">
  <PersonObjectId>11111111</PersonObjectId>
  <CellPhoneNo>070220750 </CellPhoneNo>
  <DateOfBirth>1971-03-06</DateOfBirth>
  <Email>random@sss.com </Email>
  <EMBG>00000000000</EMBG>
  <IsResident>1</IsResident>
  <FirstName>XXX</FirstName>
  <GenderTypeId>3</GenderTypeId>
  <LastName>XXX</LastName>
  <PhoneNo />
  <PlaceOfBirth />
  <IdDocumentList>
    <IdDocument>
      <IdDocumentTypeId>2</IdDocumentTypeId>
      <PlaceOfIssue>XXXX</PlaceOfIssue>
      <IdNo>XXX</IdNo>
    </IdDocument>
  </IdDocumentList>
</Person>

寫下這段代碼,我很好,但不是我需要的方式。

XmlDocument doc = new XmlDocument(); 
doc.Load(path); 
string test = doc.InnerXml.ToString(); 
var xDoc = XDocument.Parse(test);
XmlNamespaceManager mgr = new XmlNamespaceManager(xDoc.CreateNavigator().NameTable); 
mgr.AddNamespace("ns", "http://askmk/ask/ReportTypes");
var dict = xDoc.XPathSelectElement("//ns:Person", mgr) .Elements() .ToDictionary(a => a.Name.LocalName, a => a.Value);

使用此代碼,dict的值為

[0] {[PersonObjectId, 11111111]}    System.Collections.Generic.KeyValuePair<string, string>
[1] {[CellPhoneNo, 070220750      ]}    System.Collections.Generic.KeyValuePair<string, string>
[2] {[DateOfBirth, 1971-03-06]} System.Collections.Generic.KeyValuePair<string, string>
[3] {[Email, random@sss.com                                                     ]}  System.Collections.Generic.KeyValuePair<string, string>
[4] {[EMBG, 0000000000]}    System.Collections.Generic.KeyValuePair<string, string>
[5] {[IsResident, 1]}   System.Collections.Generic.KeyValuePair<string, string>
[6] {[FirstName, XXX]}  System.Collections.Generic.KeyValuePair<string, string>
[7] {[GenderTypeId, 3]} System.Collections.Generic.KeyValuePair<string, string>
[8] {[LastName, XXX]}   System.Collections.Generic.KeyValuePair<string, string>
[9] {[PhoneNo, ]}   System.Collections.Generic.KeyValuePair<string, string>
[10]    {[PlaceOfBirth, ]}  System.Collections.Generic.KeyValuePair<string, string>
[11]    {[IdDocumentList, XXXX                                        XXX]}

我要在此處更改的是第11個節點,我希望將其替換為其中的節點。 我想要

[11]    {[IdDocumentList, XXXX                                        XXX]}

被替換

[11]{[IdDocumentTypeId,2]}
[12]{[PlaceOfIssue,XXXX]}
[13]{[IdNo,XXX]}

有人可以指出我該怎么做嗎?

 var root = doc.SelectSingleNode("//ns:Person", mgr);
        foreach(var item in root.ChildNodes)
        {
            var ele = item as XmlElement;
            if (ele.Name.Equals("IdDocumentList"))
            {
                var docment = ele.FirstChild;
                docment.FirstChild.InnerText = "2";
            }
        }

        doc.Save("new.xml");

使用Xml Linq。 這不是很簡單。 它必須分兩部分完成。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XElement person = doc.Descendants().Where(x => x.Name.LocalName == "Person").FirstOrDefault();
            XNamespace ns = person.GetDefaultNamespace();

            Dictionary<string, string> dict = person.Elements()
                .Where(x => x.Name.LocalName != "IdDocumentList")
                .GroupBy(x => x.Name.LocalName, y => y == null? "" : (string)y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            foreach (XElement element in person.Descendants(ns + "IdDocument").FirstOrDefault().Elements())
            {
                dict.Add(element.Name.LocalName, (string)element);
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM