C#将XML显示为Web API Web服务

时间:2017-05-12 14:53:18

标签: c# xml asp.net-web-api

我有一个外部xml文件,其格式如下,我希望在Web服务中返回相同的格式

<properties>
  <ID>1</ID>
  <configName>name</configName>
  <config>test</config>
</properties>

C#代码:

public String Index()
    {
        var configLoc = WebConfigurationManager.AppSettings["configLocation"];
        var path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString(), configLoc);
        //string myXML = System.IO.File.ReadAllText(path);
        XmlDocument doc = new XmlDocument();
        doc.Load(path);
        return doc.InnerXml;
    }

问题在于我将XML作为字符串返回,只包含一般的“字符串”节点。

我认为我的问题与InnerXML有关,但我不确定如何尊重XML节点。如何让Web格式看起来与XML文件相同?

1 个答案:

答案 0 :(得分:0)

我能够把这个想出来......

Traceback (most recent call last):
  File "project/venv/lib/python3.6/site-packages/django/core/handlers/base.py",
 line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "project/venv/lib/python3.6/site-packages/django/core/handlers/base.py",
 line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "project/venv/lib/python3.6/site-packages/django/views/generic/base.py",
 line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "project/venv/lib/python3.6/site-packages/django/utils/decorators.py", l
ine 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "project/venv/lib/python3.6/site-packages/django/views/decorators/cache.
py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "project/venv/lib/python3.6/site-packages/django/utils/decorators.py", l
ine 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
****************** login decorators, views, ... 
  File "project/***.py", line ***, in ***
    for client in clients:
  File "project/venv/lib/python3.6/site-packages/django/db/models/query.py", li
ne 258, in __iter__
    self._fetch_all()
  File "project/venv/lib/python3.6/site-packages/django/db/models/query.py", li
ne 1076, in _fetch_all
    self._prefetch_related_objects()
  File "project/venv/lib/python3.6/site-packages/django/db/models/query.py", li
ne 656, in _prefetch_related_objects
    prefetch_related_objects(self._result_cache, self._prefetch_related_lookups)
  File "project/venv/lib/python3.6/site-packages/django/db/models/query.py", li
ne 1457, in prefetch_related_objects
    obj_list, additional_lookups = prefetch_one_level(obj_list, prefetcher, lookup, level)
  File "project/venv/lib/python3.6/site-packages/django/db/models/query.py", li
ne 1556, in prefetch_one_level
    prefetcher.get_prefetch_queryset(instances, lookup.get_current_queryset(level)))
  File "project/venv/lib/python3.6/site-packages/django/db/models/fields/relate
d_descriptors.py", line 539, in get_prefetch_queryset
    queryset = queryset.filter(**query)
  File "project/venv/lib/python3.6/site-packages/django/db/models/query.py", li
ne 790, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "project/venv/lib/python3.6/site-packages/django/db/models/query.py", li
ne 802, in _filter_or_exclude
    "Cannot filter a query once a slice has been taken."
AssertionError: Cannot filter a query once a slice has been taken.