Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从xml元素c中选择节点#_C#_Xml - Fatal编程技术网

C# 从xml元素c中选择节点#

C# 从xml元素c中选择节点#,c#,xml,C#,Xml,我想做点什么,但我不知道怎么做。Myxml看起来像这样 <Person xmlns="http://askmk/ask/ReportTypes"> <PersonObjectId>11111111</PersonObjectId> <CellPhoneNo>070220750 </CellPhoneNo> <DateOfBirth>1971-03-06</DateOfBirth> <Emai

我想做点什么,但我不知道怎么做。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>
使用此代码,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]{[IdDocumentTypeId,2]}
[12]{[PlaceOfIssue,XXXX]}
[13]{[IdNo,XXX]}

有人能告诉我怎么做吗?

使用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);
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
XElement person=doc.subjects()。其中(x=>x.Name.LocalName==“person”).FirstOrDefault();
XNamespace ns=person.GetDefaultNamespace();
Dictionary dict=person.Elements()
.Where(x=>x.Name.LocalName!=“IdDocumentList”)
.GroupBy(x=>x.Name.LocalName,y=>y==null?”:(字符串)y)
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
foreach(person.substands(ns+“IdDocument”).FirstOrDefault().Elements()中的XElement元素)
{
dict.Add(element.Name.LocalName,(string)元素);
}
}
}
}

使用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);
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
XElement person=doc.subjects()。其中(x=>x.Name.LocalName==“person”).FirstOrDefault();
XNamespace ns=person.GetDefaultNamespace();
Dictionary dict=person.Elements()
.Where(x=>x.Name.LocalName!=“IdDocumentList”)
.GroupBy(x=>x.Name.LocalName,y=>y==null?”:(字符串)y)
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
foreach(person.substands(ns+“IdDocument”).FirstOrDefault().Elements()中的XElement元素)
{
dict.Add(element.Name.LocalName,(string)元素);
}
}
}
}
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);
            }
        }
    }
}
 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");