小编Min*_*ing的帖子

删除空XML标记

我正在寻找一种可以有效地从XML中删除空标签的好方法.您有什么推荐的吗?正则表达式?的XDocument?XmlTextReader的?

例如,

const string original = 
    @"<?xml version=""1.0"" encoding=""utf-16""?>
    <pet>
        <cat>Tom</cat>
        <pig />
        <dog>Puppy</dog>
        <snake></snake>
        <elephant>
            <africanElephant></africanElephant>
            <asianElephant>Biggy</asianElephant>
        </elephant>
        <tiger>
            <tigerWoods></tigerWoods>       
            <americanTiger></americanTiger>
        </tiger>
    </pet>";
Run Code Online (Sandbox Code Playgroud)

可能成为:

const string expected = 
    @"<?xml version=""1.0"" encoding=""utf-16""?>
        <pet>
        <cat>Tom</cat>
        <dog>Puppy</dog>        
        <elephant>                                              
            <asianElephant>Biggy</asianElephant>
        </elephant>                                 
    </pet>";
Run Code Online (Sandbox Code Playgroud)

.net c# xml linq-to-xml

12
推荐指数
2
解决办法
2万
查看次数

json.net需要在json中找不到的属性

我正在使用Json.net,我得到了一个类如下

public class RecordAlias
    {   
        [JsonProperty(PropertyName = "eId", Required = Required.Always)]
        public string EntityId { get; set; }

        [JsonProperty(PropertyName = "aId", Required = Required.AllowNull)]
        public string AliasId { get; set; }

        [JsonProperty(PropertyName = "iSd", Required = Required.AllowNull)]
        public bool IsSelected { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

因此,即使通过某些项目在json字符串中没有属性"iSd",也可以对json进行反序列化,我希望如果不存在则应该填充该类型的默认值,例如,IsSelected应该为false,除了最后一项

      [{
        "eId" : "30022004",
        "aId" : "1"
    }, {
        "eId" : "30021841",
        "aId" : "1"
    }, {
        "eId" : "30021848",
        "aId" : "1"
        "iSd" : true
    }
]
Run Code Online (Sandbox Code Playgroud)

知道我怎么能实现这个目标?

c# json json.net

9
推荐指数
2
解决办法
1万
查看次数

MVC3中的Ninject会话范围概念

我正在使用Ninject框架构建MVC3应用程序.我有一个耗时的服务初始化,最后这个服务将有一个包含用户特定信息的对象,然后我需要重新使用该服务,只要用户会话是活动的,这样我可以避免一次又一次地初始化该服务

所以我的问题是

当我使用Ninject绑定服务时,我应该选择哪种范围,Ninject中的每个范围都没有会话,那么实现该要求的最佳方法是什么?还是我走错了方向?

我已经为我的一个服务创建了一个自定义提供程序,它将根据从当前Controller.User.Identity.Name中获取的用户名详细信息创建服务.下面的代码不起作用,因为缺少userName局部变量,如何通过Ninject将用户名值传递给我的自定义提供程序,以便我可以从IContext中获取它?

public class TfsConnectionManagerProvider : Provider<TfsConnectionManager>
    {
        protected override TfsConnectionManager CreateInstance(IContext context)
        {
            Uri serverUri = new Uri(ConfigurationHelper.TfsServerUrl);
            // Connect to the server without impersonation
            using (TfsTeamProjectCollection baseUserConnection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(serverUri))
            {
                // Get the identity management service
                IIdentityManagementService ims = baseUserConnection.GetService<IIdentityManagementService>();

                // Get the identity to impersonate
                TeamFoundationIdentity identity = ims.ReadIdentity
                (
                    IdentitySearchFactor.AccountName,
                    userName,  //NOTE: How can I get user name value from IContext???
                    MembershipQuery.None,
                    ReadIdentityOptions.None
                );

                // Connect using the impersonated identity
                using (TfsTeamProjectCollection impersonatedConnection = …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc ninject

8
推荐指数
2
解决办法
2793
查看次数

简单的linq需要

我有一个Link类型的Linq对象数组,其中包含如下值:

new Link {SourceId = 1, TargetId=22223}
new Link {SourceId = 1, TargetId=2221223}
new Link {SourceId = 1, TargetId=222}
new Link {SourceId = 2, TargetId=26556}
new Link {SourceId = 2, TargetId=264}
new Link {SourceId = 2, TargetId=262}
new Link {SourceId = 2, TargetId=29}

class Link
{
    public int SourceId { get; set; }
    public int TargetId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我需要一个LINQ语句来输出Dictionary<int, List<int>>包含以下内容的字典:

所述不同SourceId的键和的列表TargetId与该键作为值相关联.

非常感谢.

c# linq

4
推荐指数
1
解决办法
128
查看次数

标签 统计

c# ×4

.net ×1

asp.net-mvc ×1

json ×1

json.net ×1

linq ×1

linq-to-xml ×1

ninject ×1

xml ×1