我想做点什么,但我不知道怎么做。 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]}
有人能指出我该怎么做吗?
答案 0 :(得分:2)
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");
答案 1 :(得分:0)
使用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);
}
}
}
}