Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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#_Xml_Linq To Xml_Xelement - Fatal编程技术网

使用C#创建解析XML的对象

使用C#创建解析XML的对象,c#,xml,linq-to-xml,xelement,C#,Xml,Linq To Xml,Xelement,我花了1.5天的时间试图创建一个解析XML的对象: 签字人 johndoe@gmail.com 多伊,约翰 完整的 1001 签字人 maryjane@gmail.com 简,玛丽 发送 1002 完整的 25b9b7e8-c4c0-4711-a80c-24663f0dc6ed 网址 假的 http://google.com 列表 假的 蓝色 项目ID 假的 2. 您可以这样做: var doc = XElement.Load("path to your XML file"); var re

我花了1.5天的时间试图创建一个解析XML的对象:


签字人
johndoe@gmail.com
多伊,约翰
完整的
1001
签字人
maryjane@gmail.com
简,玛丽
发送
1002
完整的
25b9b7e8-c4c0-4711-a80c-24663f0dc6ed
网址
假的
http://google.com
列表
假的
蓝色
项目ID
假的
2.
您可以这样做:

var doc = XElement.Load("path to your XML file");
var req = new Request
{
    Id = (string)doc.Element("Id"),
    Status = (string)doc.Element("Status"),
    Url = (string)doc.Element("CustomFields")
                     .Elements("CustomFields")
                     .Where(cf => (string)cf.Element("Name") == "Url")
                     .Select(cf => (string)cf.Element("Value"))
                     .First(),
    //Follow `Url` example above to populate `List` and `ItemId` properties
    Signers = doc.Elements("RecipientStatuses")
                 .Elements("RecipientStatus")
                 .Select(o => new Signer
                 {
                     Type = (string)o.Element("Type"),
                     //Follow `Type` example above to populate `Email` and `UserName` properties
                     UserId = (int)o.Element("CustomFields").Element("CustomField")
                 })
                 .ToList()
};
使用XPath表达式的替代形式:

var doc = XElement.Load("path to your XML file");
var req = new Request
{
    Id = (string)doc.XPathSelectElement("Id"),
    Status = (string)doc.XPathSelectElement("Status"),
    Url = (string)doc.XPathSelectElement("CustomFields/CustomFields[Name='Url']/Value"),
    //Follow `Url` example above to populate `List` and `ItemId` properties
    Signers = doc.XPathSelectElements("RecipientStatuses/RecipientStatus")
                 .Select(o => new Signer
                 {
                     Type = (string)o.Element("Type"),
                     //Follow `Type` example above to populate `Email` and `UserName` properties
                     UserId = (int)o.XPathSelectElement("CustomFields/CustomField")
                 })
                 .ToList()
};
尝试单个查询

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);
            Request request = doc.Elements("EnvelopeStatus").Select(x => new  Request() {
                Id = (string)x.Element("Id"),
                Status = (string)x.Element("Status"),
                Url = (string)x.Descendants("CustomField").Where(y => (string)y.Descendants("Name").FirstOrDefault() == "Url").Select(z => z.Element("Value")).FirstOrDefault(),
                ItemId = (int)x.Descendants("CustomField").Where(y => (string)y.Descendants("Name").FirstOrDefault() == "ItemId").Select(z => z.Element("Value")).FirstOrDefault(),
                List = (string)x.Descendants("CustomField").Where(y => (string)y.Descendants("Name").FirstOrDefault() == "List").Select(z => z.Element("Value")).FirstOrDefault(),
                Signers = x.Descendants("RecipientStatus").Select(y => new Signer() {
                    Type = (string)y.Descendants("Type").FirstOrDefault(),
                    Email = (string)y.Descendants("Email").FirstOrDefault(),
                    UserName = (string)y.Descendants("UserName").FirstOrDefault(),
                    UserId = (int)y.Descendants("CustomField").FirstOrDefault()
                }).ToList()
            }).FirstOrDefault();
        }
    }
    public class Request
    {
        public string Id { get; set; }
        public string Status { get; set; }
        public string Url { get; set; }
        public string List { get; set; }
        public int ItemId { get; set; }
        public List<Signer> Signers { get; set; }
    }
    public class Signer
    {
        public string Type { get; set; }
        public string Email { get; set; }
        public string UserName { get; set; }
        public int UserId { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
Request Request=doc.Elements(“信封状态”)。选择(x=>newrequest(){
Id=(字符串)x.Element(“Id”),
Status=(字符串)x.Element(“Status”),
Url=(字符串)x.substands(“自定义字段”)。其中(y=>(字符串)y.substands(“名称”).FirstOrDefault()==“Url”)。选择(z=>z.Element(“值”)。FirstOrDefault(),
ItemId=(int)x.substands(“CustomField”)。其中(y=>(string)y.substands(“Name”).FirstOrDefault()==“ItemId”)。选择(z=>z.Element(“Value”)。FirstOrDefault(),
List=(string)x.substands(“CustomField”)。其中(y=>(string)y.substands(“Name”).FirstOrDefault()==“List”)。选择(z=>z.Element(“Value”)。FirstOrDefault(),
Signers=x.substands(“RecipientStatus”)。选择(y=>newsigner(){
类型=(字符串)y.subjects(“类型”).FirstOrDefault(),
Email=(字符串)y.s(“Email”).FirstOrDefault(),
UserName=(字符串)y.subjections(“UserName”).FirstOrDefault(),
UserId=(int)y.subjections(“CustomField”).FirstOrDefault()
})托利斯先生()
}).FirstOrDefault();
}
}
公共类请求
{
公共字符串Id{get;set;}
公共字符串状态{get;set;}
公共字符串Url{get;set;}
公共字符串列表{get;set;}
公共int ItemId{get;set;}
公共列表签名者{get;set;}
}
公共类签名者
{
公共字符串类型{get;set;}
公共字符串电子邮件{get;set;}
公共字符串用户名{get;set;}
public int UserId{get;set;}
}
}