我正在寻找一种可以有效地从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) 我正在使用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)
知道我怎么能实现这个目标?
我正在使用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) 我有一个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
与该键作为值相关联.
非常感谢.