创建这样的 XML 的正确方法是:

发布于 2024-09-05 19:02:12 字数 935 浏览 1 评论 0原文

我想在运行时创建这样的东西:

  <CWS>
    <Case name="10-040-00022">
      <CaseDetailsSet>
        <CaseDetail title="Patient name" /> 
        <CaseDetail title="Date of birth" /> 
      </CaseDetailsSet>
    </Case>
  </CWS>

所以我写了这样的东西(我希望在.NET中使用DOM..而不是XMLWriter等)

    XmlDocument doc = new XmlDocument();

    XmlElement root = doc.CreateElement("CWS");
    XmlElement singleCase = doc.CreateElement("Case");

    root.AppendChild(singleCase);

    singleCase.SetAttribute("name", "10-040-00022");

    XmlElement CaseDetailsSet = doc.CreateElement("CaseDetailsSet");
    singleCase.AppendChild(CaseDetailsSet);

    XmlElement CaseDetail = doc.CreateElement("CaseDetail");
    CaseDetailsSet.AppendChild(CaseDetail);
    CaseDetail.SetAttribute("title", "Patient Name");

请看一下它并告诉我我是否有问题,请考虑我编写的用于创建上述结构的代码。

非常感谢。

I want to create something like this at run-time:

  <CWS>
    <Case name="10-040-00022">
      <CaseDetailsSet>
        <CaseDetail title="Patient name" /> 
        <CaseDetail title="Date of birth" /> 
      </CaseDetailsSet>
    </Case>
  </CWS>

so I wrote something like this ( I wish to use DOM in .NET .. not the XMLWriter,etc)

    XmlDocument doc = new XmlDocument();

    XmlElement root = doc.CreateElement("CWS");
    XmlElement singleCase = doc.CreateElement("Case");

    root.AppendChild(singleCase);

    singleCase.SetAttribute("name", "10-040-00022");

    XmlElement CaseDetailsSet = doc.CreateElement("CaseDetailsSet");
    singleCase.AppendChild(CaseDetailsSet);

    XmlElement CaseDetail = doc.CreateElement("CaseDetail");
    CaseDetailsSet.AppendChild(CaseDetail);
    CaseDetail.SetAttribute("title", "Patient Name");

please have a look at it and tell me if I am oing something wrong , regardign the code I worte to create that structure above.

much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

殊姿 2024-09-12 19:02:12

有两件事:

  1. 您需要将根附加到 XmlDocument。
  2. 您需要添加第二个 CaseDetail。

    XmlDocument doc = new XmlDocument();
    
    XmlElement root = doc.CreateElement("CWS");
    
    doc.AppendChild(根); // 将根元素追加到 XmlDocument
    
    XmlElement singleCase = doc.CreateElement("Case");
    
    root.AppendChild(singleCase);
    
    singleCase.SetAttribute("名称", "10-040-00022");
    
    XmlElement CaseDetailsS​​et = doc.CreateElement("CaseDetailsS​​et");
    singleCase.AppendChild(CaseDetailsS​​et);
    
    XmlElement CaseDetail = doc.CreateElement("CaseDetail");
    CaseDetailsS​​et.AppendChild(CaseDetail);
    CaseDetail.SetAttribute("标题", "患者姓名");
    
    // 添加第二个案例详细信息
    XmlElement CaseDetailDateOfBirth = doc.CreateElement("CaseDetail");
    CaseDetailsS​​et.AppendChild(CaseDetailDateOfBirth);
    CaseDetailDateOfBirth.SetAttribute("头衔", "出生日期");
    

Two things:

  1. You'll need to append the root to the XmlDocument.
  2. You need to add the second CaseDetail.

    XmlDocument doc = new XmlDocument();
    
    XmlElement root = doc.CreateElement("CWS");
    
    doc.AppendChild(root); // Append the root element to the XmlDocument
    
    XmlElement singleCase = doc.CreateElement("Case");
    
    root.AppendChild(singleCase);
    
    singleCase.SetAttribute("name", "10-040-00022");
    
    XmlElement CaseDetailsSet = doc.CreateElement("CaseDetailsSet");
    singleCase.AppendChild(CaseDetailsSet);
    
    XmlElement CaseDetail = doc.CreateElement("CaseDetail");
    CaseDetailsSet.AppendChild(CaseDetail);
    CaseDetail.SetAttribute("title", "Patient Name");
    
    // add the second case detail
    XmlElement CaseDetailDateOfBirth = doc.CreateElement("CaseDetail");
    CaseDetailsSet.AppendChild(CaseDetailDateOfBirth);
    CaseDetailDateOfBirth.SetAttribute("title", "Date of birth");
    
送君千里 2024-09-12 19:02:12

只是想展示如何使用 Linq-to-XML 来做到这一点

XElement doc = new XElement("CWS",
    new XElement("Case",
        new XAttribute("name", "10-040-00022"),
        new XElement("CaseDetailSet",
            new XElement("CaseDetail",
                new XAttribute("title", "Patient name")),
            new XElement("CaseDetail",
                new XAttribute("title", "Date of birth")))));

Just thought I'd show how to do this with Linq-to-XML

XElement doc = new XElement("CWS",
    new XElement("Case",
        new XAttribute("name", "10-040-00022"),
        new XElement("CaseDetailSet",
            new XElement("CaseDetail",
                new XAttribute("title", "Patient name")),
            new XElement("CaseDetail",
                new XAttribute("title", "Date of birth")))));
看透却不说透 2024-09-12 19:02:12

我没有发现你的代码有问题。如果它创建了你想要的 xml,那就没问题了。创建 xml 文档的方法有很多种,你的方法似乎没问题。

I don't see a problem with your code. If it creates the xml you want, it should be ok. There are many different ways of creating xml documents, yours seems to be okay.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文