【问题标题】:How to use XDocument to update existing xml file which has namespace requirements?如何使用 XDocument 更新具有命名空间要求的现有 xml 文件?
【发布时间】:2020-03-28 17:25:48
【问题描述】:

我的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns="http://a01_data_navin" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://a01_data_navin event.xsd">
  <Event>
    <eventid>1</eventid>
    <Photo>
      <filepath>files\images\memory rep02.png</filepath>
      <location>
        <lat>35.496456056584158</lat>
        <lon>-99.228515625</lon>
      </location>
      <datetimestamp>2020-03-29T00:00:00</datetimestamp>
    </Photo>
  </Event>
  <Event>
    <eventid>2</eventid>
    <Photo>
      <filepath>files\images\poop.jpeg</filepath>
      <location>
        <lat>36.137874718407268</lat>
        <lon>-89.6044921875</lon>
      </location>
      <datetimestamp>2020-03-29T00:00:00</datetimestamp>
    </Photo>
  </Event>
</Root>

当我在 C# 中使用 XDocument 时

 XDocument xml = XDocument.Load(_xmlFilePath);
            // create xml structure
            var photo_XML =  tempPhotoList.ToArray();

            xml.Element("Root")?.Add(
                new XElement("Event",
                    from photo in photo_XML
                    select new XElement("eventid", photo.EventId),
                    from photo1 in photo_XML 
                    select new XElement("Photo",
                        new XElement("filepath", photo1.FileNameForPath),
                        new XElement("location", 
                            new XElement("lat", photo1.GetLatitude()), 
                            new XElement("lon", photo1.GetLongitude())),
                            new XElement("datetimestamp", photo1.DateTimeStamp)
                    ))
            );

当我运行上述代码时,我似乎无法进入 xml 文件并迭代树。我必须添加这样的命名空间:

XDocument xml = XDocument.Load(_xmlFilePath);
            // create xml structure
            var photo_XML =  tempPhotoList.ToArray();

            xml.Element(NameSpace+"Root")?.Add(
                new XElement("Event",
                    from photo in photo_XML
                    select new XElement("eventid", photo.EventId),
                    from photo1 in photo_XML 
                    select new XElement("Photo",
                        new XElement("filepath", photo1.FileNameForPath),
                        new XElement("location", 
                            new XElement("lat", photo1.GetLatitude()), 
                            new XElement("lon", photo1.GetLongitude())),
                            new XElement("datetimestamp", photo1.DateTimeStamp)
                    ))
            );

加上Element(NameSpace+"Root");我能够遍历我的 xml 文件并添加一个新事件,但我最终得到了这个..

<Event xmlns="">
    <eventid>3</eventid>
    <Photo>
      <filepath>files\images\poop.jpeg</filepath>
      <location>
        <lat>17.140790393316649</lat>
        <lon>1.7578125</lon>
      </location>
      <datetimestamp>2020-03-29T00:00:00</datetimestamp>
    </Photo>
  </Event>

我需要一些关于如何将新事件添加或更新到现有 xml 文件中的帮助,该文件具有要处理的命名空间;在 C# 中使用 XDocument?似乎使用 XDocument 可以完成我的任务。

我卡住了伙计们..请帮忙..

【问题讨论】:

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


    【解决方案1】:

    尝试以下:

                XDocument xml = XDocument.Load(_xmlFilePath);
                XElement root = xml.Root;
                XNamespace ns = root.GetDefaultNamespace();
    
    
                root.Add(new XElement(ns + "Event",
    

    【讨论】:

    • 谢谢你,这解决了我的问题。你是一个很棒的人。感谢您的帮助。
    【解决方案2】:

    大家好,这就是我为解决手头问题所做的工作..

    *我在每个 XElement 中添加了命名空间,而不仅仅是在根目录中。抱歉之前的菜鸟错误.. :P

             XElement root = xml.Root;
             XNamespace ns = root.GetDefaultNamespace();
                root.Add(
                    new XElement(ns+"Event",
                        from photo in photo_XML
                        select new XElement(ns +"eventid", photo.EventId),
                        from photo1 in photo_XML 
                        select new XElement(ns +"Photo",
                            new XElement(ns +"filepath", photo1.FileNameForPath),
                            new XElement(ns +"location", 
                                new XElement(ns +"lat", photo1.GetLatitude()), 
                                new XElement(ns +"lon", photo1.GetLongitude())),
                                new XElement(ns +"datetimestamp", photo1.DateTimeStamp)
                        ))
                );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-31
      • 2012-01-14
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      相关资源
      最近更新 更多