构建XDocument时出错

时间:2015-06-22 10:40:10

标签: c# xml linq xpath linq-to-xml

使用以下示例xml包含一个副本:

<Persons>
  <Person>
    <PersonID>7506</PersonID>
    <Forename>K</Forename>
    <Surname>Seddon</Surname>
    <ChosenName />
    <MiddleName />
    <LegalSurname />
    <Gender>Male</Gender>
  </Person>
  <Person>
    <PersonID>6914</PersonID>
    <Forename>Clark</Forename>
    <Surname>Kent</Surname>
    <ChosenName>Clark</ChosenName>
    <MiddleName />
    <LegalSurname>Kent</LegalSurname>
    <Gender>Male</Gender>
  </Person>
  <Person>
    <PersonID>6914</PersonID>
    <Forename>Clark</Forename>
    <Surname>Kent</Surname>
    <ChosenName>Clark</ChosenName>
    <MiddleName />
    <LegalSurname>Kent</LegalSurname>
    <Gender>Male</Gender>
  </Person>
</Persons>

我有以下代码,我正在尝试使用XPath查询的输出构建一个XDocument来过滤重复的元素:

string outputXml = null;
var original = XDocument.Parse(xmlString);
string xpathFilterDups = "//Person[not(PersonID = following::Person/PersonID)]";
XDocument people = new XDocument("Persons", original.XPathSelectElements(xpathFilterDups));
outputXml = people.ToString();

我收到错误:

  

类型&#39; System.ArgumentException&#39;的异常发生在System.Xml.Linq.dll中但未在用户代码中处理

无法将空白字符添加到内容中。

在这一行:

XDocument people = new XDocument("Persons", original.XPathSelectElements(xpath));

我做错了什么? : - \

1 个答案:

答案 0 :(得分:3)

您几乎可以忽略所有代码,问题就在于此:

- (void)registerForKeyboardNotifications {

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];

}

- (void)keyboardWasShown:(NSNotification*)aNotification {
}


- (void)keyboardWillBeHidden:(NSNotification*)aNotification{
}

您无法创建包含字符串的XDocument people = new XDocument("Persons"); ,您需要添加元素:

XDocument