当XML节点具有值时,似乎无法弄清楚为什么我得到NullReferenceException。我是否可以正确访问该节点?
当尝试遍历子节点时,我在下面的行上得到一个空引用异常。我希望它很小,我将继续阅读/搜索答案,但我认为询问可能会更快。非常感谢您提供任何帮助,谢谢!
variable += childNode.SelectSingleNode(".//my:AP1SupportingDelDetailsTextBox", nsManager).InnerText + "\n";
parenttxt / xml
<my:AP1ExpectedMilestones
xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2018-07-23T12:40:33">
<my:AP1SupportingActionTextBox>Action1</my:AP1SupportingActionTextBox>
<my:AP1SupportingActionHasDeliverablesCheckBox>true</my:AP1SupportingActionHasDeliverablesCheckBox>
<my:AP1AssociatedDelSection>
<my:AP1AssociatedDelRepeatingTable>
<my:AP1SupportingDelDetailsTextBox>Action1del1</my:AP1SupportingDelDetailsTextBox>
<my:AP1SupportingDelDatePick>2018-09-28</my:AP1SupportingDelDatePick>
</my:AP1AssociatedDelRepeatingTable>
<my:AP1AssociatedDelRepeatingTable>
<my:AP1SupportingDelDetailsTextBox>Action1del2</my:AP1SupportingDelDetailsTextBox>
<my:AP1SupportingDelDatePick>2018-09-28</my:AP1SupportingDelDatePick>
</my:AP1AssociatedDelRepeatingTable>
<my:AP1AssociatedDelRepeatingTable>
<my:AP1SupportingDelDetailsTextBox>Action1Del3</my:AP1SupportingDelDetailsTextBox>
<my:AP1SupportingDelDatePick>2018-09-28</my:AP1SupportingDelDatePick>
</my:AP1AssociatedDelRepeatingTable>
</my:AP1AssociatedDelSection>
</my:AP1ExpectedMilestones>
<my:AP1ExpectedMilestones
xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2018-07-23T12:40:33">
<my:AP1SupportingActionTextBox>Action2</my:AP1SupportingActionTextBox>
<my:AP1SupportingActionHasDeliverablesCheckBox>true</my:AP1SupportingActionHasDeliverablesCheckBox>
<my:AP1AssociatedDelSection>
<my:AP1AssociatedDelRepeatingTable>
<my:AP1SupportingDelDetailsTextBox>Action2Del1</my:AP1SupportingDelDetailsTextBox>
<my:AP1SupportingDelDatePick>2018-09-28</my:AP1SupportingDelDatePick>
</my:AP1AssociatedDelRepeatingTable>
<my:AP1AssociatedDelRepeatingTable>
<my:AP1SupportingDelDetailsTextBox>Action2Del2</my:AP1SupportingDelDetailsTextBox>
<my:AP1SupportingDelDatePick>2018-09-28</my:AP1SupportingDelDatePick>
</my:AP1AssociatedDelRepeatingTable>
</my:AP1AssociatedDelSection>
</my:AP1ExpectedMilestones>
childtxt / xml
<my:AP1AssociatedDelRepeatingTable
xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2018-07-23T12:40:33">
<my:AP1SupportingDelDetailsTextBox>Action1del1</my:AP1SupportingDelDetailsTextBox>
<my:AP1SupportingDelDatePick>2018-09-28</my:AP1SupportingDelDatePick>
</my:AP1AssociatedDelRepeatingTable>
<my:AP1AssociatedDelRepeatingTable
xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2018-07-23T12:40:33">
<my:AP1SupportingDelDetailsTextBox>Action1del2</my:AP1SupportingDelDetailsTextBox>
<my:AP1SupportingDelDatePick>2018-09-28</my:AP1SupportingDelDatePick>
</my:AP1AssociatedDelRepeatingTable>
<my:AP1AssociatedDelRepeatingTable
xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2018-07-23T12:40:33">
<my:AP1SupportingDelDetailsTextBox>Action1Del3</my:AP1SupportingDelDetailsTextBox>
<my:AP1SupportingDelDatePick>2018-09-28</my:AP1SupportingDelDatePick>
</my:AP1AssociatedDelRepeatingTable>
<my:AP1AssociatedDelRepeatingTable
xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2018-07-23T12:40:33">
<my:AP1SupportingDelDetailsTextBox>Action2Del1</my:AP1SupportingDelDetailsTextBox>
<my:AP1SupportingDelDatePick>2018-09-28</my:AP1SupportingDelDatePick>
</my:AP1AssociatedDelRepeatingTable>
<my:AP1AssociatedDelRepeatingTable
xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2018-07-23T12:40:33">
<my:AP1SupportingDelDetailsTextBox>Action2Del2</my:AP1SupportingDelDetailsTextBox>
<my:AP1SupportingDelDatePick>2018-09-28</my:AP1SupportingDelDatePick>
</my:AP1AssociatedDelRepeatingTable>
调试代码
[WebMethod]
public void showXML(XmlDocument doc)
{
// declare namespaces
XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
//nsManager.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2018-08-16T17:55:49");
nsManager.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2018-07-23T12:40:33");
nsManager.AddNamespace("dfs", "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution");
// declare the document for parsing
XmlNode root = doc.DocumentElement;
// parse the initiatives
XmlNodeList initiatives = root.SelectNodes("/dfs:IPDocument/my:myFields/my:AP1Main/my:AP1MainRepeatingSection", nsManager);
foreach (XmlNode node in initiatives)
{
// parse the nodes to injest data into the Expected MileStones table
XmlNodeList expected_milestones = root.SelectNodes(".//my:AP1ExpectedMilestones", nsManager);
string parenttxt = "" ;
string childtxt = "";
string variable = "";
foreach (XmlNode milestones_node in expected_milestones)
{
parenttxt += milestones_node.OuterXml;
string deliverables = milestones_node.SelectSingleNode(".//my:AP1AssociatedDelSection", nsManager);
foreach (XmlNode childNode in deliverables.ChildNodes)
{
childtxt += childNode.OuterXml;
variable += childNode.SelectSingleNode(".//my:AP1SupportingDelDetailsTextBox", nsManager).InnerText + "\n";
}
}
File.WriteAllText(@"C:\Windows\Temp\xml\outputparent.xml", parenttxt);
File.WriteAllText(@"C:\Windows\Temp\xml\outputchild.xml", childtxt);
File.WriteAllText(@"C:\Windows\Temp\xml\outputvariable.xml", variable);
}
}