0

添加空间名称时,我无法生成 xml 字符串。这就是我想生成我的xml的方式

xml 示例:

<Feedback xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Record>
        <ID>2FAC636E-F96C-4465-9272-760BAF73C0DF</QRCodeID>
        <SubID>10B5236C-47FD-468D-B88D-D789CA0C663A</SubmissionID>
        <UserID>1</UserID>
        <Page>1</Page>
    </Record>
    <Record>
        <ID>219C462B-B874-4408-AFBA-CA727922D50F</QRCodeID>
        <SubID>10B5236C-47FD-468D-B88D-D789CA0C663A</SubmissionID>
        <UserID>1</UserID>
        <Page>2</Page>
    </Record>
</Feedback>

我的代码现在的样子:

XDocument xdoc = new XDocument(
            new XElement("Feedback xmlns:i='http://www.w3.org/2001/XMLSchema-instance'",
                new XElement("Record",
                    new XElement("ID", idGuid),
                    new XElement("SubID", subGuid),
                    new XElement("UserID", 2),
                    new XElement("Page", pages)
                    )
            )
        );

当我运行它在这里抛出一个错误"Feedback xmlns:i='http://www.w3.org/2001/XMLSchema-instance'",它不喜欢字符''

4

3 回答 3

0

看看这个 MSDN 页面,它解释了你需要知道的一切

于 2013-07-02T17:56:21.840 回答
0

我很确定您需要使用Namespaces

这是试图用你的整个字符串创建一个标记,这是无效的。

于 2013-07-02T17:56:32.237 回答
0

此处的文档建议类似以下内容应该起作用:

XDocument xdoc = new XDocument(
    new XElement("Feedback",
        new XAttribute(XNamespace.Xmlns + "i", "http://www.w3.org/2001/XMLSchema-instance"),
        new XAttribute(XNamespace.Xmlns + "j", "http://schemas.sitename.com/2013/03/Project.Models"),
        new XElement("Record",
            new XElement("ID", idGuid),
            new XElement("SubID", subGuid),
            new XElement("UserID", 2),
            new XElement("Page", pages)
        )
    )
);
于 2013-07-02T17:56:40.057 回答