0

I wanna do something but i dont have idea how. Myxml looks like this

<Person xmlns="http://askmk/ask/ReportTypes">
  <PersonObjectId>11111111</PersonObjectId>
  <CellPhoneNo>070220750 </CellPhoneNo>
  <DateOfBirth>1971-03-06</DateOfBirth>
  <Email>[email protected] </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>

With this code written down, i am good but not the way i need it.

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

With this code the value of dict is

[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, [email protected]                                                     ]}  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]}

What i want to change here is the 11th node, and i want it to be replaced with the nodes inside it. I want

[11]    {[IdDocumentList, XXXX                                        XXX]}

to be replaced with

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

Can someone point me on how to do it?

2 Answers 2

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");
Sign up to request clarification or add additional context in comments.

Comments

0

Using Xml Linq. This is not very straight forward. It has to be done in two pieces. :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.