技术高手 -
我正在从第三方api捕获数据。我正在简化这个xml,通过存储过程发送到mssql。我不能总是依赖于where子句中的第一个property.Name.LocalName将真正成为第一个返回的元素。对于最后一个元素也是如此。不幸的是,我编写了错误的假设。
这是缩写逻辑:
XmlReader reader = XmlReader.Create(nodeReader, settings);
XElement data = XElement.Load(reader);
XmlNameTable table = reader.NameTable;
XmlNamespaceManager manager = new XmlNamespaceManager(table);
manager.AddNamespace("ns", "http://www.w3.org/2005/Atom");
manager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
manager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
manager.AddNamespace("base", "https://api20.ourthirdparty.com:4400/odata/v2/");
foreach (XElement entries in data.XPathSelectElements("./ns:entry", manager))
{
foreach (var contents in (from entry in entries.Elements() where entry.Name.LocalName.Contains("content") select entry))
{
foreach (var properties in (from content in contents.Elements() where content.Name.LocalName.Contains("properties") select content))
{
foreach (var property in (from property in properties.Elements()
where property.Name.LocalName.Contains("startDate")
|| property.Name.LocalName.Contains("userId")
|| property.Name.LocalName.Contains("seqNumber")
|| property.Name.LocalName.Contains("fte")
select property)
)
这里是为mssql服务器使用简化Xml的地方
// get all the instances the positions w/tags and values
sb.Append(string.Format("<{0}>{1}</{0}>", property.Name.LocalName, property.Value, property.Name.LocalName));
// add the root tag sets
docEmpJobs = begtag + sb.ToString() + endtag;
}
}
}
以下是关于开始/结束标记放置的假设不正确的地方 -
// Correct missing tags with regex
Regex tag1 = new Regex(@"<startDate>");
Regex tag2 = new Regex(@"</fte>");
docEmpJobs = tag1.Replace(docEmpJobs, "<EmpJob><startDate>");
docEmpJobs = tag2.Replace(docEmpJobs, "</fte></EmpJob>");
// Correct ampersand xml conversion failures
// Note: The ampersand was an ill-placed value from a test case, but still made it to the raw xml.
docEmpJobs = docEmpJobs.Replace("&", "and"); // return scrubbed string for xdoc conversion
有没有人对如何检测EmpJob节点的第一个标签集和最后一个标签集有任何建议?