繁体   English   中英

如何获取不带转义字符的xml? (JavaScript,角度)

[英]How to get the xml WITHOUT escape characters? (javascript, angular)

我在javascript世界中很新。 所以,请忍受我。 :-)

用户按下“保存客户订单”按钮(会将xml文件下载到用户PC):

orders.component.html:

... 
    <form (ngSubmit)="saveOrdersAsXml()" novalidate>
...
    <div class="col-sm-12 text-right">
        <button type="submit" class="btn btn-primary btn-lg" >Save Customer Orders</button> 
    </div>
...

问题是:如何在没有转义字符的情况下获取xml ?:

orders.component.ts:

export class OrderComponent implements OnInit 
{
    ...

    private saveOrdersAsXml($event) 
    {
        this.orderService.GetOrdersAsXml(this.customerNumber)
            .subscribe(res => 
            {
                // res = "Response with status: 200 OK for URL: http://localhost:52511/..."
                // res._body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>..."      
                // _body contains the xml. How do I get the xml without escape characters??

                let xml: string = res;
                const file = new Blob([xml], { type: 'text/xml;charset=utf-8' }); 
                saveAs(file, this.customerNumber + '.xml'); // Currently the file will contain "Response with status: 200 OK for URL: http://localhost:52511/..." - I need it to be xml.
            }, error => this.errorMessage = <any>error);
    }
}

orders.service.ts:

@Injectable()
export class OrderService 
{
    ...

    public GetOrdersAsXml(customerNumber: string): Observable<any> 
    {
        const dataResult = this._http.get("http://localhost:52511/api/orders/getordersasxml/" + customerNumber, , this.authenticationService.jwt()))
            .map((response: Response) => response)
            .catch(this.handleError);
        return dataResult;
    }
}

OrdersController.cs:

[Produces("application/json")]
[Route("api/orders")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class OrdersController : Controller
{
    ...

    [HttpGet("getordersasxml/{customerNumber}")]
    public string GetOrdersAsXml(string customerNumber)
    {
        // Here the xml is fine (e.g.: "<?xml version="1.0" encoding="UTF-8"?>...")
        return _orderBL.GetOrdersAsXml(customerNumber);
    }
}

编辑:经过5个小时的工作,我仍然不明白。

我试图返回JObject,HttpResponseMessage,byte []和xDocument。 越来越令人沮丧了...

当我像这样使用xDocument时:

[HttpGet("getordersasxml/{customerNumber}")]
public XDocument GetOrdersAsXml(string customerNumber)
{
    // Here the xml is fine (e.g.: "<?xml version="1.0" encoding="UTF-8"?>...")
    var xml = _orderBL.GetOrdersAsXml(customerNumber);
    return XDocument.Parse(xml);
}

在前端,我这样做:

private saveOrdersAsXml($event) 
{
    this.orderService.GetOrdersAsXml(this.customerNumber)
        .subscribe(res => 
        {
            console.log("1: " + xmlResult);
            console.log("2: " + xmlResult._body);
            console.log("3: " + new XMLSerializer().serializeToString(xmlResult._body));

            ...
        }, error => this.errorMessage = <any>error);
}

控制台返回以下内容:

1:状态响应:URL 200 OK ...

2:{“?xml”:{“ @ version”:“ 1.0”,“ @ encoding”:“ utf-16”},“客户” ...

错误TypeError:无法在'XMLSerializer'上执行'serializeToString':参数1不是'Node'类型的。 订阅者位于SafeSubscriber.next(Subscriber.ts:204)的SafeSubscriber .__ tryOrUnsub(Subscriber.ts:254)处的SafeSubscriber.searchService.GetXMLFile.subscribe._this.errorMessage [as _next](license.component.ts:111)。 _next(Subscriber.ts:135)位于CatchSubscriber.Subscriber._next(Subscriber.ts:135)位于CatchSubscriber.Subscriber.next(Subscriber.ts:95)在CHttpSubscriber.Subscriber._next(Subscriber.ts:135)位于XMLHttpRequest.onLoad(位于Object.onInvokeTask(core.umd.js:3913)的ZoneDelegate.invokeTask(zone.js:425)的http.umd.js:1259)

为什么做这么简单的任务(从后端到前端发送xml字符串)如此简单?

我现在去看牙医,但是稍后再回来。 任何帮助表示赞赏。

终于成功了。 显然,去看牙医很有帮助;-)

我这样做:

OrdersController.cs:

[HttpGet("getordersasxml/{customerNumber}")]
public string GetOrdersAsXml(string customerNumber)
{
    //return _orderBL.GetOrdersAsXml(customerNumber); // Gives trouble!

    // Put xml as a byte array into a json
    var xml = _orderBL.GetOrdersAsXml(customerNumber);
    var response = JsonConvert.SerializeObject(new
    {
        data = Encoding.UTF8.GetBytes(xml)
    }); 
    return response;
}

orders.component.ts:

private saveOrdersAsXml($event) 
{
    this.orderService.GetOrdersAsXml(this.customerNumber)
        .subscribe(res => 
        {
            // let xml: string = res; // Won't work

            // Parse json-string to json object and convert the data byte array to a string.
            let xml:string = atob(JSON.parse(res).data); 

            const file = new Blob([xml], { type: 'text/xml;charset=utf-8' }); 
            saveAs(file, this.customerNumber + '.xml'); 
        }, error => this.errorMessage = <any>error);
}

希望有人觉得这有帮助。 :-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM